Welcome to the new Golem Cloud Docs! 👋
Documentation
HTTP API Definition Binding Types

HTTP Worker Binding Types

Overview

The Worker Binding Types are a collection of different ways that a worker can be exposed to the outside world as part of an api definition.

To get a basic introduction of what api-definitions are and how bindings fit into them, please read the defining custom apis guide.

Rib Scripts

Bindings can execute code as part of various steps in the request lifecycle. All of these scripts use a scripting language called rib. You can find an introduction to the language here.

Default Binding

The default and most basic binding type that allows invoking workers in custom ways.

Example:

worker-name: '"foo"'
component-id: "{component_id}"
component-version: { component_version }
response: |
  let x = golem:it/api.{{checkout}}();
  let status: u64 = 200;
  {{headers: {{ContentType: "json", userid: "foo"}}, body: "foo", status: status}}

Parameters:

ParameterRequiredTypeDescription
componentId.componentIdtruestringID of the component to invoke
componentId.versiontruenumberVersion of the component to invoke
workerNamefalseribScript used to compute the name of the worker that will be invoked. The script has access to the request input. If not provided, an ephemeral worker is used.
idempotencyKeyfalseribScript used to compute the idempotency key. The script has access to the request input. If not provided an idempotency key will be generated.
responsetrueribScript that is used to compute the response that will be returned to the user. The script has access to the request and worker inputs and can call worker functions. The result of the script has to be an object with status and optional headers and body fields.

Cors Binding Type

This binding type is used to add CORS support to an api. To use this binding type, install it to a separate route with the same path as your main api and an OPTIONS method. Any OPTIONS requests will be automatically answered with the configured cors headers.

Any requests to your actual api will also include the CORS response headers.

The documentation you are already familiar with CORS and what the function of each of the headers are. For more information regarding CORS, see the mdn docs (opens in a new tab).

Example:

id: users-api
version: 0.0.1
createdAt: 2024-08-21T07:42:15.696Z
routes:
  - method: Options
    path: /users/{user-id}
    binding:
      bindingType: cors-preflight
  - method: Get
    path: /users/{user-id}
    binding:
      response: |
        {status: 200u64, body: "Hello World"}

Parameters:

ParameterRequiredTypeDescription
allowOriginfalsestringValue of the Access-Control-Allow-Origin header. Defaults to "*" if not provided.
allowMethodsfalsestringValue of the Access-Control-Allow-Methods header. Defaults to "GET, POST, PUT, DELETE, OPTIONS" if not provided.
allowHeadersfalsestringValue of the Access-Control-Allow-Headers header. Defaults to "Content-Type, Authorization" if not provided.
exposeHeadersfalsestringValue of the Access-Control-Expose-Headers.
maxAgefalsestringValue of the Access-Control-Max-Age header.
allowCredentialsfalsestringValue of the Access-Control-Allow-Credentials header.

File Server

A binding type that is used to retrieve files from a workers filesystem. For read-write files the file is retrieved between running worker invocation, so users are guaranteed to not see any in-progress changes to files.

For read-only files the files are retrieved from a separate datastore and not directly from the worker. This makes retrieving read-only files very cheap and allows it be used for serving static content.

Example:

bindingType: file-server
componentId:
  componentId: "{component_id}"
  version: 0
workerName: 'let name: string = "{worker_name}"; name'
response: 'let file: string = request.path.file; "/files/${{file}}"'

Parameters:

ParameterRequiredTypeDescription
componentId.componentIdtruestringID of the component to invoke
componentId.versiontruenumberVersion of the component to invoke
workerNamefalseribScript used to compute the name of the worker that will be invoked. The script has access to the request input. If not provided, an ephemeral worker is used.
idempotencyKeyfalseribScript used to compute the idempotency key. The script has access to the request input. If not provided an idempotency key will be generated.
responsetrueribScript that is used to compute the file to retrieve and the response to send to users. See the dedicatedc section for more details.

File Server Response Object

The object returned from the response rib script warrants some extra mention. There are two different structures that are supported by golem:

  1. Returning a single string. The returned string is interepreted as an absolute path and looked up in the worker filesystem and returned with an inferred content type and a 200 status code. If the file does not exist, a 404 is returned instead.

  2. As an object. This object can have 3 different fields that are explained below:

    FieldRequiredRib TypeDescription
    headersfalserecordHeaders that should be included in the response.
    statusfalseu64Status code of the response. If not provided defaults to 200.
    file-pathtruestringPath of the file that should be retrieved from the worker filesystem. If the file does not exist a 404 is returned

Http Handler

A binding type used for integrating with components that implement the wasi:http/proxy world. The binding will forward the incoming requests to the worker and return the responses back to the user.

Example:

bindingType: http-handler
componentId:
  componentId: "{component_id}"
  version: 0
workerName: 'let name: string = "{worker_name}"; name'
response: 'let file: string = request.path.file; "/files/${{file}}"'

Parameters:

ParameterRequiredTypeDescription
componentId.componentIdtruestringID of the component to invoke
componentId.versiontruenumberVersion of the component to invoke
workerNamefalseribScript used to compute the name of the worker that will be invoked. The script has access to the request input. If not provided, an ephemeral worker is used.
idempotencyKeyfalseribScript used to compute the idempotency key. The script has access to the request input. If not provided an idempotency key will be generated.