Sometimes you need synthetic data from an API, instead of from a database. For example, when you develop UIs or microservices, you might never interact directly with a relational database. You might get all of your data from APIs.
Within a Fabricate database, Fabricate allows you to mock those APIs and use synthetic data from the Fabricate database in your mock responses.
Tonic Fabricate mock APIs specifically allow you to interact with snapshots of a Fabricate-generated database.

What is a mock API?
Let's start with a quick definition of a mock API.
A mock API provides a simulation of a real service. It includes definitions of endpoints and responses from that service.
During development and testing, you can send requests to the mock API, which returns the same response that you would get from the real API.
Why use a mock API?
What are the benefits of using a mock API?
Developer empowerment and control
A mock API gives a developer full control over the server response and the underlying datastore.
This ensures fast, realistic responses during development. It also makes it very easy to recreate specific edge cases and likely failure conditions that might be harder to recreate in the real APIs.
Support for parallel development
An organization might have multiple developers working on multiple different projects at the same time.
When each developer has their own mock API and data store, they won't inadvertently affect each other's work like they might in a single environment.
Replacement for unavailable, unreliable, or complex services
In some cases, developers simply don't have access to the real-world services they are testing. It might be a third-party service that they don't have an account for, or a production service that they are blocked from accessing.
Even if they do have access, that access might require a prohibitively complicated and expensive setup. And those services, because of their complexity, might have a slow response time or might not respond reliably.
With a mock API, developers can quickly reproduce a real-world environment without having to provision servers.
Generate synthetic data and mock APIs for full coverage testing and development.
Setting up a mock API in Fabricate
In Fabricate, for a given database, you define the mock API, then create a data snapshot of the database for the mock API to send the requests to.
Define the mock API
You define the Mock API from the API tab of the database details.
The API tab displays a commented-out example of a mock API definition.

Creating the definition manually
One option is to create the definition directly. You set up each available call as a mock API route. The syntax is inspired by Express. You add each route to the api object, which supports the following methods:
- get
- post
- put
- patch
- delete
- all
For example, the following definition sets up calls to get a list of users and to get an individual user based on their identifier:
api
.get('/users', (req, res, { db }) => {
// ...
})
.get('/users/:id', (req, res, { db }) => {
// ...
})
You can also set up responses to each method.
For more information about the Fabricate mock API syntax, go to the Mock API reference in the Fabricate User Guide.
Using AI to generate the definition
Instead of defining the mock API manually, you can tell Fabricate's AI what you want, and it generates the definition for you.
To use AI to generate the definition, click Compose with AI. Provide a summary of the calls you want, then click Generate. To add the results to your mock API, click Add to API.

Create a database snapshot
Now that you've defined your mock API, you need data to run the API against. To do this, you create a snapshot of the Fabricate database.
To create your first database snapshot, click Create a DB Snapshot. After that, to add a snapshot, click the snapshot dropdown, then click Create New Snapshot.

You can use the SQL option to run queries against a snapshot, and you can download a snapshot to a .sqlite file.
Call the mock API
You make calls to your mock API at:
https://fabricate.tonic.ai/api/v1/workspaces/<workspace name>/databases/<database name>/api
For authentication, the request includes an authorization header with your Fabricate API key - Bearer <your API key>
The routes are defined under the route path. For example, to call the '/users' route, you use the following URL:
https://fabricate.tonic.ai/api/v1/workspaces/<workspace name>/databases/<database name>/api/users
Ready to try Tonic Fabricate?
A mock API allows developers to quickly set up a temporary environment where they can issue real API requests and receive a realistic response.
Using a mock API can streamline development by giving developers greater control over their testing environment and avoiding issues with system access and parallel development.
A Fabricate mock API sets up requests and responses that you can use to interact with a Fabricate database snapshot.
Want to learn more about Tonic Fabricate? Get started for free or connect with our team today.