LogoLogo
Release notesDocs homeFabricateTonic.ai
  • Tonic Fabricate User Guide
  • Fabricate workflow
  • Tutorial videos
  • Fabricate account
    • Getting started with Fabricate
    • Fabricate license plans
    • Managing your Fabricate account and profile
    • Managing users in your account
  • Databases
    • Supported database types
    • Creating and managing databases
  • Backing up and restoring the database definition
  • Configuring database variables
  • Exporting data from a database
  • Tables and columns
    • Managing database tables
      • Configuring table settings
      • Adding a table to a database
      • Removing a table from a database
      • Attaching static data to a table
      • Regenerating table data
  • Managing table columns
    • Adding and removing columns
    • Configuring a column
    • Generator reference
      • Calculated or related values
      • Data type and specific values
      • Names and other identifying information
      • Telephone numbers and email addresses
      • Geographic locations
      • Air travel
      • Natural science
      • Networks and files
      • Banking and finance
      • Dates and times
      • Vehicles
      • Companies and products
      • Healthcare and health insurance
      • Languages
      • Movies
      • Education
  • Views
    • Creating and managing views
    • Views reference
  • Workspaces
    • About workspaces
  • Creating and managing workspaces
  • Database mock API
    • About mock APIs
    • Defining a mock API
    • Creating and querying database snapshots
  • Mock API reference
  • Fabricate API and CLI
    • About the Fabricate API and CLI
    • Managing Fabricate API keys
    • Daily limits on generated data
    • Using the Fabricate API
      • Authentication for the API
      • Data model
      • Managing databases from the API
      • Generating data from the API
    • Using the Fabricate CLI
      • Setting up CLI access
      • Using the CLI to load data
Powered by GitBook
On this page
  • Routes
  • Calling the API
  • Authentication
  • Request format
  • Responses
  • JSON response
  • Plain text response
  • Set response headers
  • Set multiple response headers
  • Querying your database snapshot
  • Modifying your database snapshot
  • Generating UUIDs
Export as PDF

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

  • 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:

api.get('/users/:id', (req, res, { db }) => {
   const id = req.params.id
   
   const user = db.query(
     'SELECT * FROM users where id = ?',
     id
   )[0]

  if (!user) {
    res.status(404).json({ error: 'User not found' })
  } else {
    res.json(user)
  }
})

Responses

JSON response

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

api.get('/hello-world', (req, res, { db }) => {
  res.json({ message: 'Hello, world!' })
})

Plain text response

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

api.get('/hello-world', (req, res, { db }) => {
  res.send('Hello, world!')
})

Set response headers

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

api.get('/hello-world', (req, res, { db }) => {
  res.setHeader('Content-Type', 'text/plain')
  res.send('Hello, world!')
})

Set multiple response headers

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

api.get('/hello-world', (req, res, { db }) => {
  res.set({
    'Content-Type': 'text/plain',
    'X-Powered-By': 'Fabricate'
  })
  res.send('Hello, world!')
})

Querying your database snapshot

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

api.get('/users/:id', (req, res, { db }) => {
  const id = req.params.id
  const user = db.query('SELECT * FROM users where id = ?', id)[0]
  res.json(user)
})

Modifying your database snapshot

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

api.patch('/users/:id', (req, res, { db }) => {
  const id = req.params.id
  const { name, email } = req.body // The request body is automatically parsed as
                                   // JSON if the Content-Type request header is 
                                   //"application/json"
  db.run('UPDATE users SET name = ?, email = ? where id = ?', [name, email, id])
  res.status(200)
})

The db.run method returns an object with:

{
  changes: number, // The number of rows affected by the operation
  lastID: number // The last inserted row ID (only for insert operations)
}

Generating UUIDs

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

api.get('/users', (req, res, { db }) => {
  const id = db.run('insert into users(id) values (fab_uuid())')[0]
  res.json({ id: id })
})

Last updated 6 days ago