Mock API reference

Fabricate's Mock API syntax is inspired by Express.

Routes

To declare routes, use the api object:

api
    .get('/users', (req, res, { db }) => {
        // List the first 10 users
        const users = db.query('SELECT * FROM users limit 10');
        res.json(users)  })

To create a full API, chain multiple routes together:

api
  .get('/users', (req, res, { db }) => {
     // ...
  })
  .get('/users/:id', (req, res, { db }) => {
    // ... 
  })

The api object supports the following methods:

  • get

  • post

  • put

  • patch

  • delete

  • all

Note, however, that database snapshots are currently read-only.

Calling the API

Your API is available at:

https://fabricate.tonic.ai/api/v1/workspaces/<user name>/databases/<database name>/api

Routes are defined under this route path. So for example, to call the '/users' route, you use the following URL:

https://fabricate.tonic.ai/api/v1/workspaces/<user name>/databases/<database name>/api/users

Authentication

To authenticate your requests, send an Authorization header with the value:

Bearer <your API key>

For information on how to create and manage API keys, go to Managing Fabricate API keys.

Request format

The request object contains the following properties:

  • body - the request body as a string

  • headers - the request headers as an object

  • method - the request method

  • params - the request path parameters as an object

  • path - the request path (excluding the base URL /api/v1/databases/<database_name>/api)

  • query - the request query parameters as an object

To access query parameters, use the req.query object. To change the response status, use the res.status(code) method:

Responses

JSON response

To send a JSON response, use the res.json method:

Plain text response

To send a plain text response, use the res.send method:

Set response headers

To set response headers, use the res.setHeader method:

Set multiple response headers

To set multiple headers, use the res.set method:

Querying your database snapshot

To query your database snapshot, use the db.query method:

Modifying your database snapshot

To modify your database snapshot, use the db.run method:

The db.run method returns an object with:

Generating UUIDs

To generate a new UUID, use the fab_uuid() function. For example:

Last updated