api-virtual-cli
v1.0.0
Published
A CLI to create mock API endpoints
Maintainers
Readme
API CLI - Mock Server
A command-line interface (CLI) tool to create, manage, and serve mock API endpoints quickly and easily. Ideal for frontend developers who need a simulated backend with dynamic responses.
Features
- Local Server: Spins up a local server to serve your endpoints.
- Web User Interface: Manage your endpoints through a friendly web UI.
- Dynamic Routes: Define routes with parameters (e.g.,
/users/:id). - Dynamic Responses: The JSON response body can use variables from the route and the request body.
- HTTP Method Support: Create endpoints for GET, POST, PUT, PATCH, and DELETE.
- Integrated CLI & UI: Manage endpoints from the web and get information from the command line.
Installation
To install the tool globally via npm, run:
npm install -g api-virtual-cliCLI Usage
The tool is operated with the api-cli command.
serve
Starts the API server and the web administration interface.
api-cli serveBy default, the server runs on port 7070. You can specify a different port with the --port or -p option.
api-cli serve --port 8080Once started, you will see two URLs in your terminal:
- API Mock Server: The base URL where your endpoints will be available (e.g.,
http://localhost:7070/api/...). - Admin UI: The URL for the web interface to manage endpoints (e.g.,
http://localhost:7070).
Creating Virtual Endpoints
The primary way to create and manage endpoints is through the web administration interface.
1. The Creation Form
In the UI, you will find a form with the following fields:
- Endpoint Route: The path for your endpoint, without including
/api/. You can add dynamic parameters using the:prefix (e.g.,users/:id). - HTTP Method: Choose the method for which the endpoint will respond: GET, POST, PUT, PATCH, or DELETE.
- HTTP Status Code: The status code that the response will return (200, 404, etc.).
- JSON Response Body: The body of the response in JSON format. This is where you can use dynamic variables.
- Request Body Schema (Optional): This field appears only for POST, PUT, and PATCH. It allows you to define a schema to use variables from the request body.
2. Dynamic Responses
This is the most powerful feature. You can make your JSON response change according to the request.
Example 1: Using URL Parameters
- Route:
products/:productId - Method:
GET - Response Body:
{ "id": ":productId", "name": "Sample Product", "description": "Details for product :productId" }
If you make a GET request to /api/products/123, the response will be:
{
"id": "123",
"name": "Sample Product",
"description": "Details for product 123"
}Example 2: Using Request Body Parameters
- Route:
users - Method:
POST - Request Body Schema:
{ "name": ":name", "email": ":email" } - Response Body:
{ "status": "ok", "message": "User :name created successfully", "data": { "name": ":name", "email": ":email", "id": 12345 } }
If you make a POST request to /api/users with the body {"name": "Ana", "email": "[email protected]"}, the response will be:
{
"status": "ok",
"message": "User Ana created successfully",
"data": {
"name": "Ana",
"email": "[email protected]",
"id": 12345
}
}3. Managing Endpoints
Below the form, you will find the "Active Endpoints" table. From here you can:
- View all the endpoints you have created, with their method and path.
- Edit an existing endpoint. Clicking it will fill the form with its data for you to modify.
- Delete an endpoint you no longer need.
