Add HTTP Endpoints to Your Project

This article shows you how to create an HTTP method in your CLI project and call it from your frontend to handle HTTP requests. These endpoints handle server-side logic and can return any kind of data to your frontend.

To create an HTTP endpoint:

  1. Create an endpoint file with HTTP method handlers.
  2. Add frontend code to call the endpoint.

Step 1 | Create the endpoint file

Create the backend endpoint file that handles HTTP requests. At the end of this step, you'll have a working endpoint that can respond to GET and POST requests.

  1. Create a new file in the src/pages/api/ directory with the name <your-endpoint-name>.ts in your CLI project.

  2. Add endpoint handlers for the HTTP methods you want to support. Each handler receives a request object and returns a Response:

    Copy

Step 2 | Call the endpoint from your frontend

Call your endpoint from frontend components using Wix's built-in HTTP client. At the end of this step, you'll be able to send requests to your backend endpoint and receive responses.

  1. In your frontend component, use httpClient.fetchWithAuth() to call your endpoint:

    Copy
  2. Start your local development environment to test the endpoints:

Copy
  1. Press Dashboard to open the dashboard page in your browser, and click the buttons to execute the HTTP endpoint calls.

  2. View the logs in the CLI and browser console. You should see something like:

    CLI

    Copy

    Browser console

    Copy

Step 3 | Build and deploy your project

Build and deploy your project to make your endpoints available in production. Once deployed, your endpoints will be accessible at the production URLs and can handle live traffic from your site's visitors.

Delete an HTTP endpoint

To delete an HTTP endpoint from your project, delete the file under src/pages/api/ that contains your HTTP endpoint and build and deploy again.

Did this help?