The goal of this example SDK is to show a basic representation of generating and customizing a TypeScript SDK with Speakeasy. It is based on a simple (and fictional) CRUD API for managing recipes. Use this README as a guide to understand how the SDK is generated and customized. Links to relevant sections of the codebase are provided here in order to explore the code in action and see best practices for various customizations.
Once the prerequisites are installed and a valid OpenAPI Specification (OAS) file is available (in this case, RecipeOpenAPISpec.yaml), the following command is used to generate the SDK:
speakeasy quickstart
The CLI then prompts for the required information:
What is the location of your OpenAPI Spec file? RecipeOpenAPISpec.yaml
What is a good name for this source document? SpeakeasyRecipeBook-OAS
Which target would you like to generate? typescript
Name your SDK: speakeasy-recipe-book
If successful, an output similar to this will be displayed:
│ Workflow - success
│ └─Target: speakeasy-recipe-book - success
│ └─Source: SpeakeasyRecipeBook-OAS - success
│ └─Applying Overlays - success
│ └─Apply 3 overlay(s) - success
│ └─Validating Document - success
│ └─Diagnosing OpenAPI - success
│ └─Tracking OpenAPI Changes - success
│ └─Snapshotting OpenAPI Revision - success
│ └─Storing OpenAPI Revision - success
│ └─Computing Document Changes - success
│ └─Downloading prior revision - success
│ └─Computing changes - success
│ └─Uploading changes report - success
│ └─Validating gen.yaml - success
│ └─Generating Typescript SDK - success
│ └─Setup Environment - success
│ └─Load and Validate Document - success
│ └─Generate SDK - success
Source SpeakeasyRecipeBook-OAS Compiled Successfully
└─Lint report: https://go.speakeasy.com/5lKqS
└─API Change report: https://go.speakeasy.com/ed1Kh
SDKs Generated Successfully
└─speakeasy-recipe-book overview: https://go.speakeasy.com/CKVwn
The resulting workflow.yaml
file generated can be found here. Subsequent generations can be run at any time using speakeasy run
.
Now that the base SDK is generated, customizations for a variety of use cases can be applied. The following is a sampling of methods that can be used to customize the SDK.
Overlays are applied to extend or alter the functionality of the Recipe Book API.
- Add "Snacks" tag added to the API
- Hide an internal-only operation from public SDK and mark it as internal only
Overlays are then added as a new workflow source using:
speakeasy configure sources
Application of overlays is shown in the workflow.yaml file.
Speakeasy extensions provide fine-tuned control over the SDK, enabling modification to behaviors like retries, pagination, error handling, and other advanced SDK features.
- Use
x-speakeasy-name-override
to rename an operation in the OpenAPI Spec. - Use
x-speakeasy-enums
to add enums to an operation in the OpenAPI Spec.
Speakeasy allows customization of the paths to which models are generated and imported.
Speakeasy allows grouping of models into namespaces.
Speakeasy allows custom error handling for different status codes, such as 401 (Unauthorized) or 500 (Internal Server Error).
Speakeasy allows custom pagination rules for each API operation using the x-speakeasy-pagination
extension.
Is the guide missing a particular example that would be helpful? If so, email [email protected] with your feedback!
Recipe Book API: A simple (and fictional) API to manage recipes for a Recipe Book.
- SDK Installation
- Requirements
- SDK Example Usage
- Available Resources and Operations
- Standalone functions
- Retries
- Error Handling
- Server Selection
- Custom HTTP Client
- Debugging
The SDK can be installed with either npm, pnpm, bun or yarn package managers.
npm add https://github.com/speakeasy-sdks/recipe-book
pnpm add https://github.com/speakeasy-sdks/recipe-book
bun add https://github.com/speakeasy-sdks/recipe-book
yarn add https://github.com/speakeasy-sdks/recipe-book zod
# Note that Yarn does not install peer dependencies automatically. You will need
# to install zod as shown above.
For supported JavaScript runtimes, please consult RUNTIMES.md.
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
const speakeasyRecipeBook = new SpeakeasyRecipeBook();
async function run() {
const result = await speakeasyRecipeBook.status.getRecipeStatus({
id: "<id>",
});
// Handle the result
console.log(result);
}
run();
Available methods
- getAll - Get all recipes
- create - Create a new recipe
- getById - Get a recipe by ID
- update - Update an existing recipe by ID
- delete - Delete a recipe by ID
- getRecipeStatus - Get the status of a recipe
All the methods listed above are available as standalone functions. These functions are ideal for use in applications running in the browser, serverless runtimes or other environments where application bundle size is a primary concern. When using a bundler to build your application, all unused functionality will be either excluded from the final bundle or tree-shaken away.
To read more about standalone functions, check FUNCTIONS.md.
Available standalone functions
recipesCreate
- Create a new reciperecipesDelete
- Delete a recipe by IDrecipesGetAll
- Get all recipesrecipesGetById
- Get a recipe by IDrecipesUpdate
- Update an existing recipe by IDstatusGetRecipeStatus
- Get the status of a recipe
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a retryConfig object to the call:
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
const speakeasyRecipeBook = new SpeakeasyRecipeBook();
async function run() {
const result = await speakeasyRecipeBook.status.getRecipeStatus({
id: "<id>",
}, {
retries: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
});
// Handle the result
console.log(result);
}
run();
If you'd like to override the default retry strategy for all operations that support retries, you can provide a retryConfig at SDK initialization:
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
const speakeasyRecipeBook = new SpeakeasyRecipeBook({
retryConfig: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
});
async function run() {
const result = await speakeasyRecipeBook.status.getRecipeStatus({
id: "<id>",
});
// Handle the result
console.log(result);
}
run();
All SDK methods return a response object or throw an error. By default, an API error will throw a custom_errors.SDKError
.
If a HTTP request fails, an operation my also throw an error from the models/custom_errors/httpclienterrors.ts
module:
HTTP Client Error | Description |
---|---|
RequestAbortedError | HTTP request was aborted by the client |
RequestTimeoutError | HTTP request timed out due to an AbortSignal signal |
ConnectionError | HTTP client was unable to make a request to a server |
InvalidRequestError | Any input used to create a request is invalid |
UnexpectedClientError | Unrecognised or unexpected error |
In addition, when custom error responses are specified for an operation, the SDK may throw their associated Error type. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation. For example, the getAll
method may throw the following errors:
Error Type | Status Code | Content Type |
---|---|---|
custom_errors.AuthError | 401 | application/json |
custom_errors.SDKError | 4XX, 5XX | */* |
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
import {
AuthError,
SDKValidationError,
} from "speakeasy-recipe-book/models/custom_errors";
const speakeasyRecipeBook = new SpeakeasyRecipeBook();
async function run() {
let result;
try {
result = await speakeasyRecipeBook.recipes.getAll({
page: 461008,
});
// Handle the result
console.log(result);
} catch (err) {
switch (true) {
case (err instanceof SDKValidationError): {
// Validation errors can be pretty-printed
console.error(err.pretty());
// Raw value may also be inspected
console.error(err.rawValue);
return;
}
case (err instanceof AuthError): {
// Handle err.data$: AuthErrorData
console.error(err);
return;
}
default: {
throw err;
}
}
}
}
run();
Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The SDKValidationError
that is thrown as a result will capture the raw value that failed validation in an attribute called rawValue
. Additionally, a pretty()
method is available on this error that can be used to log a nicely formatted string since validation errors can list many issues and the plain error string may be difficult read when debugging.
You can override the default server globally by passing a server index to the serverIdx
optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
# | Server | Variables |
---|---|---|
0 | https://api.recipeBook.com/v1 |
None |
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
const speakeasyRecipeBook = new SpeakeasyRecipeBook({
serverIdx: 0,
});
async function run() {
const result = await speakeasyRecipeBook.status.getRecipeStatus({
id: "<id>",
});
// Handle the result
console.log(result);
}
run();
The default server can also be overridden globally by passing a URL to the serverURL
optional parameter when initializing the SDK client instance. For example:
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
const speakeasyRecipeBook = new SpeakeasyRecipeBook({
serverURL: "https://api.recipeBook.com/v1",
});
async function run() {
const result = await speakeasyRecipeBook.status.getRecipeStatus({
id: "<id>",
});
// Handle the result
console.log(result);
}
run();
The TypeScript SDK makes API calls using an HTTPClient
that wraps the native
Fetch API. This
client is a thin wrapper around fetch
and provides the ability to attach hooks
around the request lifecycle that can be used to modify the request or handle
errors and response.
The HTTPClient
constructor takes an optional fetcher
argument that can be
used to integrate a third-party HTTP client or when writing tests to mock out
the HTTP client and feed in fixtures.
The following example shows how to use the "beforeRequest"
hook to to add a
custom header and a timeout to requests and how to use the "requestError"
hook
to log errors:
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
import { HTTPClient } from "speakeasy-recipe-book/lib/http";
const httpClient = new HTTPClient({
// fetcher takes a function that has the same signature as native `fetch`.
fetcher: (request) => {
return fetch(request);
}
});
httpClient.addHook("beforeRequest", (request) => {
const nextRequest = new Request(request, {
signal: request.signal || AbortSignal.timeout(5000)
});
nextRequest.headers.set("x-custom-header", "custom value");
return nextRequest;
});
httpClient.addHook("requestError", (error, request) => {
console.group("Request Error");
console.log("Reason:", `${error}`);
console.log("Endpoint:", `${request.method} ${request.url}`);
console.groupEnd();
});
const sdk = new SpeakeasyRecipeBook({ httpClient });
You can setup your SDK to emit debug logs for SDK requests and responses.
You can pass a logger that matches console
's interface as an SDK option.
Warning
Beware that debug logging will reveal secrets, like API tokens in headers, in log messages printed to a console or files. It's recommended to use this feature only during local development and not in production.
import { SpeakeasyRecipeBook } from "speakeasy-recipe-book";
const sdk = new SpeakeasyRecipeBook({ debugLogger: console });
You can also enable a default debug logger by setting an environment variable SPEAKEASYRECIPEBOOK_DEBUG
to true.
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.