Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Interfaces

setupInterfaces() defines the network interfaces your service exposes and how they are made available to the user. This function runs on service install, update, and config save.

Single Interface

For a service with one web interface, refer to the Hello World example in the Quick Start.

Multiple Interfaces

Expose multiple paths (e.g., web UI and admin panel) from the same port:

import { i18n } from './i18n'
import { sdk } from './sdk'

const uiPort = 8080

export const setInterfaces = sdk.setupInterfaces(async ({ effects }) => {
  const uiMulti = sdk.MultiHost.of(effects, 'ui-multi')
  const uiMultiOrigin = await uiMulti.bindPort(uiPort, {
    protocol: 'http',
  })

  const ui = sdk.createInterface(effects, {
    name: i18n('Web UI'),
    id: 'ui',
    description: i18n('The web interface'),
    type: 'ui',
    masked: false,
    schemeOverride: null,
    username: null,
    path: '',
    query: {},
  })

  const admin = sdk.createInterface(effects, {
    name: i18n('Admin Panel'),
    id: 'admin',
    description: i18n('Admin interface'),
    type: 'ui',
    masked: false,
    schemeOverride: null,
    username: null,
    path: '/admin/',
    query: {},
  })

  const uiReceipt = await uiMultiOrigin.export([ui, admin])
  return [uiReceipt]
})

The key steps are:

  1. Create a MultiHost and bind a port with a protocol
  2. Create one or more interfaces using sdk.createInterface()
  3. Export the interfaces from the origin and return the receipt(s)

Interface Options

sdk.createInterface(effects, {
  name: i18n('Display Name'),      // Shown in UI (wrap with i18n)
  id: 'unique-id',                 // Used in sdk.serviceInterface.getOwn()
  description: i18n('Description'),// Shown in UI (wrap with i18n)
  type: 'ui',                      // 'ui' or 'api'
  masked: false,                   // Hide from discovery?
  schemeOverride: null,            // Force 'https' or 'http'?
  username: null,                  // Auth username (if any)
  path: '/some/path/',             // URL path
  query: {},                       // URL query params
})
OptionTypeDescription
namestringDisplay name shown to the user. Wrap with i18n().
idstringUnique identifier. Used to retrieve this interface in main.ts via sdk.serviceInterface.getOwn().
descriptionstringDescription shown to the user. Wrap with i18n().
type'ui' or 'api'Whether this interface is a user-facing UI or a programmatic API.
maskedbooleanIf true, the interface is hidden from service discovery.
schemeOverridestring or nullForce a specific URL scheme ('https' or 'http'). Use null to let the system decide.
usernamestring or nullUsername for basic authentication, if required.
pathstringURL path appended to the base address (e.g., '/admin/').
queryobjectURL query parameters as key-value pairs.

Tip

The id you assign to an interface is what you use in main.ts to retrieve hostnames for that interface. For example, if you set id: 'ui', you would call sdk.serviceInterface.getOwn(effects, 'ui') to get its address information. See Main for details.