http-functions: Sample Usage

This article shows a sample flow that calls an HTTP function using the http-functions module.

Add the following code to your http-functions.js file in your site's backend:

Copy
import { ok, notFound } from "wix-http-functions"; import { members } from "wix-members.v2"; export async function get_member(request) { try { // Get member data. const member = await members.getCurrentMember(); return ok({ member }); } catch (error) { // Handle error. return notFound({ error }); } }

Add the following code in your app to call your HTTP function:

Copy
import { OAuthStrategy, createClient } from "@wix/sdk"; import { functions } from "@wix/http-functions"; // Create a client. const client = createClient({ auth: OAuthStrategy({ clientId: "{YOUR_HEADLESS_CLIENT_ID}" }), modules: { functions }, }); async function getCurrentMember() { try { // Call your HTTP function. // Use the get() method // because the HTTP function has the 'get' prefix. const response = await client.functions.get("member"); const data = await response.json(); const { member } = data; // Handle member data. } catch (error) { // Handle error. } } getCurrentMember();
Did this help?