Custom Generated Client
Learn how to create a custom-generated client with TypeScript types for your OpenAPI specification.
Umbraco uses @hey-api/openapi-ts to generate its HTTP client for the OpenAPI specification of the Management API. It is available through the @umbraco-cms/backoffice/http-client package.
The following examples show how to generate a client from an OpenAPI specification and how to use it in the project. Below, the @hey-api/openapi-ts library is used, but the same principles apply to any other library that generates a TypeScript client.
Generate your own client
The generated client provides a convenient way to make requests to the specified API with type-safety without having to manually write the requests yourself. Consider generating a client to save time and reduce effort when working with custom API controllers.
To get started, install the generator using the following command:
npm install @hey-api/openapi-tsThen, use the openapi-ts command to generate a client from your OpenAPI specification:
npx openapi-ts --input https://example.com/openapi.json --output ./my-clientThis will generate a TypeScript client in the ./my-client folder. Import the generated client into your project to make requests to the Management API.
To learn more about OpenAPI and how to define your API specification, visit the OpenAPI Documentation.
Connecting to the Management API
Your generated client needs the correct base URL, credentials, and authentication to talk to the Management API. The recommended approach uses Hey-API's runtimeConfigPath to inherit these settings automatically from the backoffice's built-in HTTP client (umbHttpClient).
The Umbraco Extension Template already includes this setup. If you scaffolded your extension with dotnet new umbraco-extension, authentication works out of the box — no additional configuration needed.
Using runtimeConfigPath (Recommended)
runtimeConfigPath (Recommended)To pass plugin options like runtimeConfigPath, create a config file instead of using CLI flags.
Create
openapi-ts.config.tsin your project root:
Run the generator, which will pick up the config file automatically:
Create a
src/hey-api.tsfile and add the following:
This copies baseUrl, credentials, auth, and headers from the backoffice's HTTP client into your generated client at initialization time. Extensions load after the backoffice is fully configured, so all values are available.
When the generator runs, the generated client.gen.ts will automatically import createClientConfig and apply it during client creation. Your SDK functions will be authenticated without any entrypoint setup.
Passing umbHttpClient directly
umbHttpClient directlyIf you only have a few requests, you can skip client configuration entirely and pass umbHttpClient as the client parameter on each call:
This uses the backoffice's HTTP client directly for that request instead of the generated client. The umbHttpClient already has authentication and the correct base URL configured.
The auth callback on umbHttpClient only fires for SDK functions that have security metadata. This metadata is generated automatically when your OpenAPI specification includes a security scheme (for example, Bearer authentication). If your spec does not include a security scheme, the generated functions will not send an Authorization header. For direct .get() / .post() calls (without a generated client), see Umbraco HTTP Client.
Fetch API
If you only have a few requests, you can also use the fetch function directly. Read more about that here:
Further reading
Last updated
Was this helpful?