This feature is in Developer Preview and is subject to change.
Backend API extensions in the CLI allow you to define HTTP functions that can be called from your frontend code. The CLI handles the setup and hosting of the backend code for you, simplifying the development process. These API functions can be invoked from any frontend component in your CLI project.
Follow the instructions below to:
Once these tasks are complete, your app will have a backend API extension that can be called from your frontend code.
In the terminal:
Navigate to your project repo.
Run the following command:
The CLI will display a menu of extensions to generate. Under Backend Extensions, select API and hit enter to continue.
The CLI will prompt you to name the API. You can only have one API with a given name.
The CLI will prompt you to choose an API method.
Upon completion, the API extension files will be created in your project directory with the following structure:
The api.ts
file is essential and includes boilerplate GET
and POST
functions that take a Node Request
and return a Node Response
. The CLI also supports PUT
and DELETE
methods.
To test your API:
Add logging statements in your API functions:
src/backend/api/<your-api-name>/api.ts
Add code to call the API from your frontend code. For example, add a function that calls the API from a dashboard page with buttons to test the function:
Note: The endpoint is a BASE_API_URL
created by the CLI combined with the API name you chose during setup. For example, if you named your API test
, you'd replace <your-api-name>
with test
.
Start your local development environment:
Press D
to open the dashboard page in your browser, and click the buttons to execute the API calls.
View the logs in the CLI and browser console. You should see something like:
CLI:
Browser console:
Tip: In the CLI, press L
to view the full logs, C
to clear the logs, ESC
to exit the log view, and use the up and down arrow keys to navigate.
Once your app is ready for production, you can build it and create a version in the app dashboard.
Run the following command to build your app:
Run the following command and follow the prompts to create an app version:
An app version allows you to publish an app to the Wix App Market or install it on a site with a direct install URL.
For more information about building and deploying your app, see Build and Deploy an App with the CLI.
To delete an API from your app, simply delete the subfolder under src/backend/api
that contains your API extension files.