Migrate from Web Methods to HTTP Endpoints

If you're upgrading from the Legacy Wix CLI for Apps, this guide helps you migrate your existing Web method extensions to HTTP endpoints. The main changes involve updating your file structure and syntax.

Step 1 | Relocate your web method files

Your web method files are located at src/backend/<yourWebMethod>.web.ts.

In your CLI project, create a new file in the src/pages/api/ directory with the name <yourWebMethod>.ts.

Step 2 | Replace the import statement

Replace the @wix/web-methods import statement with the APIRoute type.

For example, the web method file uses this import:

Copy

HTTP endpoints use the following import:

Copy

Step 3 | Replace web method exports

  1. Replace each webMethod() export with an HTTP method handler.
  2. Each handler receives a request object. Read arguments from the request object. For example, extract values from the query string for GET requests, or from the request body for POST requests.
  3. Implement the relevant logic and return a Response object with the result.

For example, the web method file exports the following:

Copy

The corresponding HTTP endpoint exports the following:

Copy

Step 4 | (Optional) Manage permissions

Web methods specify permissions using the Permissions enum from @wix/web-methods. To restrict access in an HTTP endpoint, use Get Token Info to read the caller's token and inspect its subjectType.

Note: To enforce permissions, use httpClient.fetchWithAuth() when calling from your frontend. This passes your access token in the request to your HTTP endpoint. Without this, Get Token Info won't have a token to inspect.

For example, the following web method limits access to site members:

Copy

Enforce the same permissions in an HTTP endpoint as follows:

Copy

Step 5 | Replace web method imports in your frontend

In your frontend code, replace web method imports with HTTP calls.

For example, the web method is called from the frontend as follows:

Copy

Call an HTTP endpoint from the frontend as follows:

Copy

Full example

The following is a full example of migrating web methods to HTTP endpoints on the backend and frontend.

Backend

This example shows the backend implementation of a web method located in the src/backend/ directory:

Copy

The equivalent HTTP endpoint is located in the src/pages/api/ directory:

Copy

Frontend

This example shows a web method called from the frontend:

Copy

The equivalent HTTP endpoint is called from the frontend as follows:

Copy

See also

Did this help?