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.
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.
Replace the @wix/web-methods import statement with the APIRoute type.
For example, the web method file uses this import:
HTTP endpoints use the following import:
webMethod() export with an HTTP method handler.request object. For example, extract values from the query string for GET requests, or from the request body for POST requests.Response object with the result.For example, the web method file exports the following:
The corresponding HTTP endpoint exports the following:
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:
Enforce the same permissions in an HTTP endpoint as follows:
In your frontend code, replace web method imports with HTTP calls.
For example, the web method is called from the frontend as follows:
Call an HTTP endpoint from the frontend as follows:
The following is a full example of migrating web methods to HTTP endpoints on the backend and frontend.
This example shows the backend implementation of a web method located in the src/backend/ directory:
The equivalent HTTP endpoint is located in the src/pages/api/ directory:
This example shows a web method called from the frontend:
The equivalent HTTP endpoint is called from the frontend as follows: