Skip to content

Generate an SDK ​

Generate a typed SDK from an OpenAPI document, then call its operations with checked requests and responses.

The SDK is a client and types. The client checks and converts each request and each response, so your code never calls a conversion function. The SDK has no model classes.

Install ​

sh
npm install @exprtlabs/golem
npm install --save-dev @exprtlabs/golem-openapi-ts typescript@~7.1.0-beta

With the MobX extension, install @exprtlabs/golem-openapi-ts in dependencies, because generated files import its MobX runtime.

Requests and responses are validated by default (validation profiles). Choose how to build in Build tools. Include the generated sdk/ directory in the TypeScript program that builds your app. Ignore it in git with the other generated files.

Point to your OpenAPI document ​

Add golem.yaml at the project root. The example keeps it in server/. Paths are relative to this file. name: Provider names the client ProviderClient. This excerpt leaves out two keys. The validation root key sets runtime validation. The extensions key of the block turns on MobX.

yaml
- input: provider/provider.yaml
  output: provider/sdk
  name: Provider

The document is the contract. Keep it in the project or point input at the file that your API already owns. The generator reads the features in OpenAPI support. The example's provider.yaml declares createCharge (POST /charges), and list, get, text and event operations:

yaml
post:
  operationId: createCharge
  parameters:
    - in: header
      name: Idempotency-Key
      schema: { type: string }
  requestBody:
    required: true
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/Money'
  responses:
    '201':
      description: Created charge
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Charge'

Generation stops on an invalid document and reports each problem.

Generate the SDK ​

sh
npx golem generate

The command writes the sdk/ directory under output. The example writes provider/sdk/.

What to import ​

Import ProviderClient and the data types from the sdk/index.js file under your output directory. The example's provider/client.ts imports ./sdk/index.js. Import operation factories from the public paths in Generated files.

Golem replaces only the files it generated. Keep your own code, including models over SDK types, outside sdk/. A file there that golem did not generate stops generation.

Change provider.yaml and generate again when the contract changes.

Choose a mode ​

An SDK job has a mode key (SDK modes). The calls are the same in both modes.

ModeUse it whenWhat the SDK holds
standalone (default)You get a ready SDK and have no golem setup, or you publish the SDKThe client, the types, the checks and conversions, and a copy of the golem runtime that it uses
golemYour app runs golem and has models in the same golem.yamlThe client and the types. The models job writes the checks and conversions with your models
  • A standalone SDK imports nothing from @exprtlabs/golem. With dates: temporal, install temporal-polyfill and load it before the first call.
  • A golem mode SDK needs a model block in the same file. Add mode: golem to the job.
  • An SDK in a block with the mobx extension needs mode: golem (MobX and React).

First call ​

ts
export async function listCharges(baseUrl: string, token: string) {
  const client = new ProviderClient({ baseUrl, auth: { bearerAuth: token } });
  const operation = client.listCharges();
  return operation.execute({});
}

Set baseUrl to your service and pass credentials as in Authentication. client.listCharges() creates a reusable operation. .execute({}) sends GET /charges and returns the response. Add parameters and a body, and read the response and check the status before you read the body.

Full example: examples/payments.