Write an HTTP Function

HTTP functions define custom site APIs, which allow you to expose your site's functionality externally. This means that you, or other people, can access your site's functionality by calling your custom site APIs.

HTTP functions determine the custom site API's HTTP method, endpoint, parameters, functionality, and response.

Calling a custom site API triggers your HTTP function to run.

Step 1 | Create the file to host your HTTP functions

Create a file called http-functions.js in your site's backend folder:

  1. Start coding:

    • Wix Studio: Click on the Code icon in the left sidebar to open the code panel.
    • Wix Editor: Turn on Dev Mode.
  2. Navigate to Backend & Public > Backend, and then create a file called http-functions.js.

Tip:

You can also create the file with the following shortcut:

  1. Click on the + next to Backend.
  2. Click Expose site API.

Step 2 | Create and export your HTTP function

Your function's name must include the following parts joined by an underscore:

  • The method that should be used when calling your custom site API.
  • The functionName that should be used in your custom site API's URL.
Copy

Step 3 | Code your HTTP function

In your HTTP function, add the code you want to run when your custom site API is called. You can access the full range of functionality that you normally can when coding on a Wix site, including Wix's Velo APIs.

As with all API calls, you can interact with a custom site API call in the following ways:

Request

Use the request parameter to provide your HTTP function with information from your custom site API call, including the body, headers, and url. For all supported properties, see WixHttpFunctionRequest.

Copy

You can then access the request object in the function in the same way as you'd access a parameter in any JavaScript function.

Response

Your HTTP Function should return a response object. This is the response sent when the custom site API is called.

There are several functions you can use to create a response object. Find them in the wix-http-functions module.

The response object supports the following properties:

  • body: Defined by you in the response function.
  • headers: Defined by you in the response function.
  • status: Defined by the response function.

Step 4 | Keep your site secure (optional)

HTTP functions expose your site's data and functionality to anyone who calls your custom site API, so it's recommended to authenticate who is calling your custom site API.

One way to do so is by retrieving a secret key from the authentication header in the API request. This uses the Secrets Manager and wix-secrets-backend.

For example:

Copy

Note: If another Wix site is sending requests to your endpoints, you can use the HMAC Authentication Velo package for even more security.

Step 5 | Debug your code

Debug HTTP functions by adding console.log() calls to your function's code.

The information you log appears in the function output when using Functional Testing and in your site's Logs.

The information logged by code that runs on the backend can also be viewed as Wix Logs. Wix Logs are accessible via Developer Tools > Logging Tools on your site's dashboard.

See also

Was this helpful?
Yes
No