Dear experts,
I’m working on downloading the order data to autofill our warehouse management software. I see I can setup http-functions. When I used Postman to call I did not getting the result I expected.
{
“result”: “Works!”,
“resultLength”: 999,
“items”: {
“isFulfilled”: false,
“isRejected”: false
}
}
I have two questions :
- Is there step by step tutorial on this
- How do I create API KEY to protect access to my order data so that not just anybody can call the function?
Peter
Here’s the code I tried just to get the products :
export function get_hello(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
let nameQuery;
if (request.query) {
nameQuery = request.query.name;
}
let statusQuery = wixData.query(“Products”);
if (nameQuery){
statusQuery = statusQuery.eq(“Name”, nameQuery);
}
let resultLength;
wixData.query(“Products”)
.find()
.then( (results) => {
resultLength = results.length;
} ) ;
if (!resultLength){
resultLength = 999;
}
options.body = {
“result”: “Works!”,
“resultLength”: resultLength,
“items”: statusQuery.find(),
};
return ok(options);
}
You can use this example to get started building your own API:
Expose and Access Site APIs
Use MyApi and MyApiClient to expose and access external APIs.
Thank you for the link. I managed to create a function with that. However it seems like I can’t access my Order collection since the permission is set to Admin. Will it be possible to login as Admin first then access the Order collection?
@peter75061 You might be able to access the collection in the backend by using suppressAuth .
@yisrael-wix Thanks. There only one I can use by allowing suppressAuth is wix-data queryReferenced? Is there a way to use wix-data query() with suppressAuth option?
@yisrael-wix I’m getting Server Error on Orders collection that’s Admin permission only. But if I changed it to Products collection, it runs fine
export function get_hello(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
let optionsData = {
“suppressAuth”: true ,
“suppressHooks”: true
}
let query = wixData.query(“Stores/Orders”, optionsData);
return query
.find()
.then((r) => {
if (r.items.length > 0) {
options.body = {
“resultLength”: r.items.length,
“items”: r.items,
};
return ok(options);
}
// no matching items found
options.body = {
“error”: “not found”
};
return notFound(options);
})
}
@yisrael-wix Never mind. Thank you. I added the option in the find() function of WixDataQuery.