Wix REST API within Velo

During a recent Wix Webinar (saved in the Devs of Wix YouTube channel), there was mention made of using Wix REST API calls from within Velo to access functionality that is available with the REST APIs but not exposed with the Velo APIs. The example in the webinar was to create a Wix Bookings service that reserved 2 people/staff for each Booking reservation.

However, since the focus of the webinar was not on the use of the REST APIs, there was almost no guidance provided on how to actually implement such a call to the REST APIs from within Velo page or backend code. Can someone provide some guidance here? Wix Devs: it would be good to post the actual code used during the webinar - maybe as a Tutorial post.

Thanks in advance!

It’s a timely question, at least for me, as I need to manipulate blog entries via the REST API. It should be extremely easy to call the REST APIs from the backend. They just require some auth information and then a simple fetch call. However, the documentation seems spotty, at least one of the required fields is not marked as such, and even the API tester throws an internal 500 error saying the split function doesn’t exist.

Thanks! Something just clicked when I read your reply and I now have it working - at least for GET requests. You’re correct - just a fetch from a backend module but with authentication using API Keys as explained online.

Now on to POST requests…

Thanks!

If you don’t mind me asking. what REST API are you calling, and can you provide a code example? This seems like it should be dirt simple but there are undocumented required fields for the draft-post API. I’d like to get something/anything to work at this point. But this just throws a 500 error.

import wixSecretsBackend from 'wix-secrets-backend';
import {fetch} from 'wix-fetch';

export function addOOS(){
    wixSecretsBackend.getSecret("ManagerOOSPosts")
    .then ((mySecret) => {
        const fetchOptions = {
        method: 'post',
        headers:  {
            'content-type': 'application/json',
            'Authorization': mySecret,
            'wix-account-id': '3ce23514-62b3-4167-a04a-ad0b131b09e9', 
            'wix-site-id': 'cdf3c1b4-d8d7-45c0-9c7d-d29b1e7040a8'
            },
        body: JSON.stringify({
            'draftPost': {'title': 'Order of Service - Draft'},
            'publish': false,
            'fieldsets': [],
            'fieldMask': []
            })
        }
        
        fetch('https://www.wixapis.com/blog/v3/draft-posts', fetchOptions)
        .then( (httpResponse) => {
            console.log(httpResponse);
        })
    })
}

My current need is to query Wix Events. The Velo APIs don’t allow listing of registered Event Guests (there is a new set of APIs in “Developer Preview” but i have no idea how long it will take them to make their way to “Production” use).

I’ll be working on a POST request example tomorrow so I’ll “post” an example when I get that working. I don’t work much with blogs so it may be a POST request in the Wix Events area…

I now have a successful POST request (Query Events, under Wix Events). It takes a “search term” and “limit” from the front end and calls the backend function shown here. It returns a list of all Events with the Event Title containing the search term.

import {fetch} from 'wix-fetch';
import {getApiKey} from 'backend/secrets.jsw';

const eventsApiBaseUrl = 'https://www.wixapis.com/events';
const queryEventsUrl = '/v1/events/query';

let ownerAccountId = '<hidden>';
let siteId = '<hidden>';

export async function queryEvents(searchTerm, limit) {

    try {
        const body = {
            "filter": {
                "title": {
                "$contains": searchTerm
                }
            },
            "limit": limit
        }
        const requestBody = JSON.stringify(body);
        const accessToken = await getApiKey();
        const fetchString = eventsApiBaseUrl + queryEventsUrl; // Query Events
        const requestOptions = buildRequest('POST', accessToken, requestBody);

        console.log(fetchString);
        console.log(requestOptions);

        const httpResponse = await fetch(fetchString, requestOptions);
        console.log(httpResponse);
        if (httpResponse.ok) {
            return httpResponse.json();
        } else {
            return "Fetch did not succeed";
        }
    } catch (error) {
        console.error(error);
    }
} // end of function queryEvents

function buildRequest(method, accessToken, requestBody) {
    return {
        method: method,
        headers: {
            'Content-Type': 'application/json',
            Authorization: accessToken,
            'wix-account-id': ownerAccountId,
            'wix-site-id': siteId
        },
        body: requestBody,
    };
} // end of function buildRequest

I tried getting the Create Draft Post API to work as well, but it failed when run from my site. I did get the “API tester” to work though. The only difference was the addition of the fieldsets parameter, but when I tried adding that to my site code, it still failed. Here is the POST body that I used when it worked in the API Tester.

{
  "draftPost": {
    "title": "A Blog Test"
  },
  "publish": false,
  "fieldsets": []
}

I’m not sure why the API tester didn’t work for you, but I didn’t get the same 500 error that you reported…

1 Like

Hello, were you able to have it working in the end ?
I have same needs and get a error 400 BAD Request.

The Authorization is an APIKey right ? I cannot have an access token wihout authorization code, I cannot have an authorixation code if I did not submit my app. I need to implement my app before submitting it.

Hi, Einat from Bookings here. To call REST APIs from Velo you may use wix-fetch
you can also use: Accessing 3rd-Party Services with the Fetch API