iii

REST API

Build REST APIs with ease.

The REST API Module is a module that allows you to build REST APIs with ease.

modules::rest_api::RestApiModule

Sample Configuration

- class: modules::api::RestApiModule
  config:
    port: 3111
    host: 0.0.0.0
    cors:
      allowed_origins:
        - http://localhost:3000
        - http://localhost:5173
      allowed_methods:
        - GET
        - POST
        - PUT
        - DELETE
        - OPTIONS

Configuration

port
number

The port to listen on. Defaults to 3111.

host
string

The host to listen on. Defaults to 0.0.0.0.

cors
Cors

The CORS configuration.

Trigger Type

This Module add a new Trigger Type: api.

Sample code

bridge.registerTrigger({
  trigger_type: 'api',
  functionPath,
  config: {
    api_path: '/api/v1/users',
    http_method: 'GET',
  },
})

Request & Response Objects

ApiRequest

When an API trigger fires, the function receives an ApiRequest object:

path_params
Record<string, string>

Variables extracted from the URL path (e.g., /users/:id).

query_params
Record<string, string | string[]>

URL query string parameters.

body
any

The parsed request body (JSON).

headers
Record<string, string | string[]>

HTTP request headers.

ApiResponse

Functions must return an ApiResponse object:

status
number

HTTP status code (e.g., 200, 404, 500).

body
any

The response payload.

headers
Record<string, string>

HTTP response headers (optional).

Request Lifecycle

Example Handler

import { ApiRequest, ApiResponse } from '@iii-dev/sdk'

async function getUser(req: ApiRequest) {
  const userId = req.path_params.id
  const user = await database.findUser(userId)

  return {
    status: 200,
    body: { user },
    headers: {
      'Content-Type': 'application/json',
    },
  }
}

bridge.registerFunction({
  function_path: 'api.getUser',
  handler: getUser,
})

bridge.registerTrigger({
  trigger_type: 'api',
  function_path: 'api.getUser',
  config: {
    api_path: '/users/:id',
    http_method: 'GET',
  },
})

On this page