Skip to content

Authentication ​

Pass credentials once, and each operation applies what its security requirement asks for.

This page uses the ProviderClient of the example's payment provider. Its document, provider.yaml, declares a bearer security scheme and requires it:

yaml
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
yaml
security:
  - bearerAuth: []

Run npx golem generate. The client then has an auth option, keyed by security-scheme name. A client whose operations reference no security scheme has no auth option. If you cannot edit your document, follow the steps in Use a document you do not own.

Pass a token ​

provider/client.ts:

ts
export function createAuthorizedClient(baseUrl: string, token: string): ProviderClient {
  return new ProviderClient({
    baseUrl,
    auth: { bearerAuth: token }, // Keyed by security scheme name.
  });
}
// The request includes: Authorization: Bearer <token>
  • Your application gets and stores the token.
  • To give no credential for a scheme, leave that key out of auth. The client then tries the next security requirement.

Read the current token for each request ​

Pass a function when the token can change while the client is alive. The client calls it on each .execute().

provider/client.ts again:

ts
export function createRefreshingClient(baseUrl: string, currentToken: () => string): ProviderClient {
  // The callback runs on each execute call, so one operation picks up a refreshed token.
  return new ProviderClient({ baseUrl, auth: { bearerAuth: currentToken } });
}

The function can be async. Your application refreshes tokens and signs users in.

Other schemes ​

OpenAPI schemeValue in authRequest credential
API keyString, or function that returns a stringNamed header, query parameter or cookie
HTTP bearerString, or function that returns a stringBearer authorization header
HTTP Basic{ username, password }, or function that returns itBasic authorization header
OAuth2 and OpenID ConnectAccess token, or function that returns itBearer authorization header
  • An OAuth function receives the requested scopes and the declared flows.
  • An OpenID Connect function receives the openIdConnectUrl of the scheme.
  • Schemes in one security requirement apply together. Separate requirements are alternatives.
  • An empty requirement allows anonymous access. Operation-level security overrides document-level security.

Missing credentials ​

If no security requirement has all its credentials, .execute() rejects before it calls fetch. The rejection is a plain Error, not a LibError. Its message starts with No configured credentials satisfy.

Use a document you do not own ​

Golem reads security from the OpenAPI document. It needs two parts:

  • components.securitySchemes declares each scheme.
  • security names the schemes that the operations require. security at the root applies to every operation. security on an operation replaces it for that operation.

A document that lacks one part gives a client with no auth option. To get one:

  • Ask the owner of the document to add both parts. Then run npx golem generate again.
  • Or keep a copy of the document in your repository and add both parts to the copy. Set input of the SDK job to the copy (the openapi-ts block). Apply your parts again when the owner publishes a new version.
  • Or add the credential yourself with the headers or fetchImpl option of the client. Golem then does not know the scheme. The client has no auth option and no type check for the credential.

Mutual TLS ​

For a declared mutualTLS scheme, pass mutualTls: { fetchImpl }. Configure that transport with the client certificate first. Mutual TLS uses no token. The client sends every request through mutualTls.fetchImpl.