exposing my API to third parties


I included an HTTP-Function for a data collection I have. I want to be able to pull data from the collection by referencing the headers. For example, “milestone1” header has “complete” in row 1.

-How do I create a valid JSON to obtain this?

Thank you,

Do you want to create a link of this function to obtain the result?

Hi there :raised_hand_with_fingers_splayed:

You’re not returning the right syntax of the response, the response body must be of type string . Use JSON.stringify() to convert it into a text.

const response = {
    headers: {/*..*/},
    body: JSON.stringify(/*your body's JSON*/)
}

Hope this helps~!
Ahmad

You might also be interested in playing with the following examples:

Hi Rashid, to clarify, I want to be able to return specific data from an http call. For example, return “complete” that is in the contractDB database, for milestone1 header, using this address:

[<https://greenfinance-smartcontract.](<https://greenfinance-smartcontract.) com/_functions/api/contractDb/milestone1>

-any guidance is appreciated.

Thanks,

@marccfaria use below code in http-functions.js file and publish.

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

export function get_example(request) {
 let options = {
 "headers": {
 "Content-Type": "application/json"
 }
 };
 // query a collection to find matching items
 return wixData.query("contractDB")
 .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": `'${request.path[0]} ${request.path[1]}' was not found`
 };
 return notFound(options);
 } )
 // something went wrong
 .catch( (error) => {
      options.body = {
 "error": error
 };
 return serverError(options);
 } );
}

https://greenfinance-smartcontract. com/_functions-dev/example/
run this url in the address bar after publishing. You will get all the data from connectDB database.