> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> 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`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - 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: Add HTTP Endpoints to Your Project

## Article: Add HTTP Endpoints to Your Project

## Article Link: https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/supported-extensions/backend/http-endpoints/add-http-endpoints-to-your-project.md

## Article Content:

# 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. They're sometimes also referred to as backend APIs.

To create an HTTP endpoint:

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

<blockquote class="caution">

**Caution:** HTTP endpoints in `src/api/` aren't extensions in the framework sense. They're routes that the CLI discovers from the filesystem:

- You don't register them in [`src/extensions.ts`](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/supported-extensions/about-the-extensions-ts-file.md).
- They don't appear on the **Extensions** page in the app dashboard.

Adding or removing an endpoint file is enough, and no registration step is required.

</blockquote>

## 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/api/` directory with the name `<your-endpoint-name>.ts` in your CLI project. The endpoint's route is `/<your-endpoint-name>`.

    To start from template code, run `npm run generate` and select **HTTP Endpoint**. The generated file already has `GET` and `POST` handlers. Use the next step's code as a reference.

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

    ```ts
    import type { APIRoute } from '@wix/custom-extensions/types';

    export const GET: APIRoute = async ({ request }) => {
        console.log('Log from GET.'); // This message appears in the terminal where `wix app dev` runs.
        return new Response('Response from GET.'); // This response is visible in the browser console
    };
    
    export const POST: APIRoute = async ({ request }) => {
        const data = await request.json();
        console.log('Log POST with body: ', data); // This message appears in the terminal where `wix app dev` runs.
        return new Response(JSON.stringify(data)); // This response is visible in the browser console.
    };
    ```

## Step 2 | Call the endpoint from your frontend

> **Note:** If you came here from [Elevate API Call Permissions](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/project-development/elevate-api-call-permissions.md), complete only **Step 1** above. Skip **Step 2** and return to that article, it has its own instructions for calling 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:

    ```tsx
    import { httpClient } from '@wix/essentials';

    function Index() {
      const callEndpointGET = async () => {
        try {
          const baseApiUrl = new URL(import.meta.url).origin;
          const res = await httpClient.fetchWithAuth(`${baseApiUrl}/<your-endpoint-name>`);
          const data = await res.text();
          console.log('Response:', data);
        } catch (error) {
          console.error('Error:', error);
        }
      };

      const callEndpointPOST = async () => {
        try {
          const baseApiUrl = new URL(import.meta.url).origin;
          const res = await httpClient.fetchWithAuth(`${baseApiUrl}/<your-endpoint-name>`, {
            method: 'POST',
            body: JSON.stringify({ message: 'Hello from frontend' }),
          });
          const data = await res.json();
          console.log('Response:', data);
        } catch (error) {
          console.error('Error:', error);
        }
      };

      return (
        <WixDesignSystemProvider>
          <Page>
            <Page.Content>
              <button onClick={callEndpointGET}>Call GET Endpoint</button>
              <button onClick={callEndpointPOST}>Call POST Endpoint</button>
            </Page.Content>
          </Page>
        </WixDesignSystemProvider>
      );
    }
    ```
2. Start your local development environment to test the endpoints:

```bash
npm run dev
```

3. Open the dashboard link that the CLI prints, go to your dashboard page, and click the buttons to execute the HTTP endpoint calls.

4. View the logs. The backend logs appear in the terminal where `wix app dev` runs. In the browser console, you should see something like:

   ```
   Response: Response from GET.
   Response: {"message":"Hello from frontend"}
   ```

## Step 3 | Build and deploy your project

[Build and deploy your project](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/project-development/build-and-deploy.md) 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/api/` that contains your HTTP endpoint and build and deploy again.