How to make a api for my store DB which returns a json object

Hi,

I have online store. The DB name is products which is under stores. I want to write a code which return all the data in DB as json array object. Can anyone help me to di this.

Im using the following code

import wixStores from ‘wix-stores-backend’;
import {ok, notFound, serverError} from ‘wix-http-functions’;
import wixData from ‘wix-data’;
import wixHttpFunctions from ‘wix-http-functions’;
import {fetch} from ‘wix-fetch’;
// URL looks like:
// https://www.mysite.com/_functions/myFunction/John/Doe
// or:
// // https://user.wixsite.com/mysite/_functions/myFunction/John/Doe
export function get_myFunction(request) {

let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
// query a collection to find matching items
return request.body.json()
.then( (body) => {
wixData.get(“Products”,body)
} )

// .eq(“Name”, request.path[0])

// .find()
.then( (results) => {
// let items = results;

// matching items were found

    options.body = { 

“items”: results
}
// no matching items found

return notFound(options);
} )
// something went wrong
. catch ( (error) => {
options.body = {
“error”: error
};
return serverError(options);
} );
}

Hi,
At this moment it’s not possible with http-functions.
Roi.

So which function i can use to make an API ?? can you please tell me ??

Hi,

Unfortunately it is not yet possible to expose a product collection using http functions.
We are working on releasing this feature.

Stay tuned.

Hi,

Good news - it is now possible to expose a product collection list using http functions.

Can you please share me the code

@amandeepphilips05

The following code returns the product collection using http functions.

Do a get request on this url to test it
https://idoi240.wixsite.com/cookie/_functions/product


import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';

export function get_product(request) {
 let options = {
 "headers": {
 "Content-Type": "application/json"
    }
  };
 // query a collection to find matching items  
 return wixData.query("Stores/Products")
    .find()
    .then( (results) => {
 // matching items were found
 if(results.items.length > 0) {
        options.body = {
 "items": results.items
        };
 return ok(options);
      }
 // no matching items found
      options.body = {
 "error": `not found`
      };
 return notFound(options);
    } )
 // something went wrong
    .catch( (error) => {
      options.body = {
 "error": error
      };
 return serverError(options);
    } );
}

Any news about this release? I have a similar project. I need to expose my Products Collection under Store as a json file to be connected to DataFeedWatch server. DatafeedWatch generates a feed that synchs once a day in Merchant Center. This enables to create Google Shopping Ads using a synched Product Collection directly from Wix Store.

Is there a way to do it using http-function API or I still need to wait for it? Is really inconvenient to this as a recurring task: exporting CVS file from Products Collection, and Importing then into DataFeedWatch site.