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 stringheaders
- the request headers as an objectmethod
- the request methodparams
- the request path parameters as an objectpath
- 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