Skip to main content

Goal

Expose a Function as an HTTP endpoint so external clients can call it via REST.

Steps

1. Enable the REST API module

Make sure iii-config.yaml has the REST API module enabled:
iii-config.yaml
  - class: modules::api::RestApiModule
    config:
      port: 3111
      host: localhost
      default_timeout: 30000
      concurrency_request_limit: 1024
      cors:
        allowed_origins:
          # To allow all origins, use '*':
          # - '*'
          - localhost
        allowed_methods:
          - GET
          - POST
          - PUT
          - DELETE
          - OPTIONS

2. Register the Function

http-endpoint.ts
import {registerWorker, Logger} from 'iii-sdk'

const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134')

iii.registerFunction({ id: 'users::create' }, async (req) => {
  const logger = new Logger()
  const { name, email } = req.body
  const user = { id: crypto.randomUUID(), name, email }
  logger.info('User created', { userId: user.id })
  return { status_code: 201, body: user }
})

3. Register the HTTP trigger

http-trigger.ts
iii.registerTrigger({
  type: 'http',
  function_id: 'users::create',
  config: { api_path: '/users', http_method: 'POST' },
})

4. Try it

curl -X POST http://localhost:3111/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice", "email": "alice@example.com"}'

Result

Your Function is now accessible as POST /users on port 3111. The http trigger handles request parsing and response serialization automatically.
HTTP-triggered Functions receive an ApiRequest and should return an ApiResponse. See the SDK Reference for type details.