> Portal Navigation:
>
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt
## Resource: Write an HTTP Function
## Article: Write an HTTP Function
## Article Link: https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/write-an-http-function.md
## Article Content:
# Write an HTTP Function
HTTP functions define [custom site APIs](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/about-custom-site-apis.md), 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](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/site-api-calls.md) 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.
1. 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**.
1. 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](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/methods-for-http-functions.md) that should be used when calling your custom site API.
- The [`functionName`](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/site-api-calls.md#variables) that should be used in your custom site API's URL.
```js
export function _ () {}
```
## 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](https://dev.wix.com/docs/velo.md) and [SDK](https://dev.wix.com/docs/sdk/.md) APIs.
As with all API calls, you can interact with a custom site API call in the following ways:
- [Request](#request)
- [Response](#response)
### 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`](https://dev.wix.com/docs/velo/velo-only-apis/wix-http-functions/wix-http-function-request/introduction.md).
```js
export function _ (request) {}
```
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](https://dev.wix.com/docs/velo/velo-only-apis/wix-http-functions/wix-http-function-response/introduction.md). 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 Velo [`wix-http-functions` module](https://dev.wix.com/docs/velo/velo-only-apis/wix-http-functions/introduction.md).
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](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/developer-tools/secrets-manager/about-the-secrets-manager.md) and [`wix-secrets-backend`](https://dev.wix.com/docs/velo/apis/wix-secrets-backend/introduction.md).
For example:
```js
import { badRequest } from "wix-http-functions";
import { getSecret } from "wix-secrets-backend";
// This function compares the authorization key provided in the
// request headers with the secret key stored in the Secrets Manager.
async function isPermitted(headers) {
try {
const authHeader = headers.auth;
const sharedAuthKey = await getSecret("secretEmail");
if (authHeader === sharedAuthKey) {
return true;
}
return false;
} catch (err) {
console.error(err);
return false;
}
}
export async function get_functionName(request) {
const headers = request.headers;
if (!(await isPermitted(headers))) {
const options = {
body: {
error: "Not authorized",
},
headers: {
"Content-Type": "application/json",
},
};
return badRequest(options);
}
// Now add your code for authorized requests.
}
```
> **Note:**
If another Wix site is sending requests to your endpoints, you can use the [HMAC Authentication](https://dev.wix.com/docs/velo/articles/velo-package-readmes/wix-http-functions-hmac-authentication.md) 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](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/testing-monitoring/functional-testing/about-functional-testing.md) and in your site's [Logs](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/testing-monitoring/testing-troubleshooting/about-debugging-your-code.md#debugging-with-wix-logs).
The information logged by code that runs on the backend can also be viewed as [Wix Logs](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/developer-tools/logs/about-logs.md). Wix Logs are accessible via [**Developer Tools > Logging Tools**](https://www.wix.com/my-account/site-selector/?buttonText=Select%20Site&title=Select%20a%20Site&autoSelectOnSingleSite=true&actionUrl=https:%2F%2Fwww.wix.com%2Fdashboard%2F%7B%7BmetaSiteId%7D%7D%2Fdeveloper-tools/site-events?referralInfo=sidebar) on your site's dashboard.
## See also
- [About custom site APIs](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/about-custom-site-apis.md)
- [Methods for HTTP functions](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/methods-for-http-functions.md)
- [Custom site API calls](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/site-api-calls.md)