Use wix sdk functionalities in an Exposed API in Code Block app (http-functions)

Question:
What is the correct approach to create an app (code blocks) that exposes an API that uses the wix sdk for manipulating the store data and utilties (not the wix-dara : collecions used in the backend) , eg : query products , create products , create checkout ..
I have already tried some functions from wix-stores.v2 , @wix/stores but with no luck get 500 errors.

Please, if anyone could help, I would be very grateful.

It would be a huge help if you’re able to share the code you’ve already tried :slight_smile: - you mention HTTP Functions in blocks, so sharing this article as a starting point - Expose a Blocks App API with HTTP Functions

Yes here is my http-functions.js file :

import { ok, badRequest } from 'wix-http-functions';
import { products } from '@wix/stores';
import { elevate } from "wix-auth";

export function post_getProducts(request) {

  const response = {
        "headers": {
            "Content-Type": "application/json"
        }
    }

 const elevatedProducts = elevate(products.queryProducts().find);

 return request.body.json()
        .then((body) => {
            console.log(body);

            return elevatedProducts()
                .then(products => {

                    response.body = {
                        "response": products.items
                    }
                    return ok(response)

                })
                .catch(err => {
                   
                    response.body = {
                        "response": err
                    }
                    return badRequest(response)
                
                })
        })
        .catch(err => {
            response.body = {
                "response": err
            }
            return badRequest(response)
        })

}

Not sure if it’s the cause, but 2 things I noticed:

  • You’re currently combining SDK and Velo code (wix-auth is Velo, the equivalent in SDK is this @wix-essentials)
  • I think the use of elevate is slightly wrong. From what I understand, it should be elevate(products.queryProducts), where you then do elevatedProducts().find() later in the code

Thanks @noahlovell for follow up, actually i tried the changes you mentioned : replaced the auth.elevate from @wix-essentials and changed the declaration of elevate(products.queryProducts) but with no luck to retrieve the store products. the response iam gettting now is:

{
  "response": {
    "details": {
      "applicationError": {
        "description": "UNKNOWN",
        "code": 404,
        "data": {
          "requestId": "1751986882.868857674135313968855",
          "details": null
        }
      }
    },
    "status": 404
  }

}

I’ve always found HTTP functions a little more challenging to get setup :sweat_smile:

I’d recommend seeing if you can get it working as a default backend function (in a web.js file), where you can run the function - and then once that’s working get it working with HTTP - but from what you describe, it’s very doable.


Added a bookmark so if I have some time will try setting this up to share it :slight_smile: