Use TypeScript SDKs generated in Postman
This guide provides instructions for using TypeScript SDKs generated by Postman SDK Generator. It covers how to import the SDK into your project, run example code, and best practices for using the SDK effectively in your applications.
SDK structure
The TypeScript SDK includes the following key files:
src/index.ts— Main entry point, exports SDK client, services, and modelssrc/services/— Service classes organized in directories alongside their modelssrc/http/— HTTP client, error types, handler chain, hooks, and transport layerpackage.json— Dependencies vary by transport (fetch built-in, or axios)
The following is an example of the typical structure of a generated TypeScript SDK:
Example usage
Each generated SDK includes an examples/ directory with a working project demonstrating SDK usage.
The example includes the following features:
- SDK initialization with authentication
- Making API calls to various endpoints
- Error handling patterns
- Working with typed request/response models
Location
Run the example
Example code structure
examples/src/index.ts
Import the TypeScript SDK locally
You can import the generated SDK using npm, by file path, or using a workspace (for monorepos). Below are instructions for each method.
Import using npm link
You can use npm link to create a symlink to the generated SDK, allowing you to import it as if it were installed from npm.
-
Run the following commands in the SDK directory:
Alternatively, run the following commands in your project directory:
-
Use the SDK in your code.
Import using a file path
To import the SDK from a local path, you can either install it using npm or add it as a dependency in your package.json.
-
Using npm:
-
Adding to package.json:
Import using workspaces (for monorepos)
To import using workspaces, add the SDK path to the workspaces array in your root package.json:
Publish to npm
To share your generated TypeScript SDK with the community, you can publish it to npm. This allows other developers to easily install it using npm. Follow the steps below.
Prerequisites
To publish your SDK to npm, you need the following:
- An npm account. You can create it at npmjs.com.
- npm CLI installed. You can install it with Node.js from nodejs.org.
- A unique package name. You can check availability using the
npm search your-package-namecommand.
Publish your TypeScript SDK to npm
-
Configure
package.json. Ensure these fields are set correctly: -
Build the SDK.
-
Log in to npm.
-
Publish the package.
-
For public packages, use:
-
For scoped private packages, use:
-
-
Verify publication:
Publish updates
To publish an updated SDK, update the version in the package.json following the semantic versioning:
- Patch:
1.0.0→1.0.1(bug fixes) - Minor:
1.0.0→1.1.0(new features, backward compatible) - Major:
1.0.0→2.0.0(breaking changes)
Alternatively, you can use the npm version command:
Best practices for publishing TypeScript SDKs
Use the following best practices when publishing your TypeScript SDK to npm:
- Use
.npmignoreto exclude unnecessary files. - Include a comprehensive
README.md. - Add a
LICENSEfile. - Tag releases in Git:
git tag v1.0.0 && git push --tags. - Set up CI/CD for automated publishing.
- Consider using
nptool for streamlined publishing:npx np.
Advanced usage
You can further customize the SDK by modifying the generated code, adding new features, or integrating it with your existing codebase. Here are some advanced topics to explore.
Authentication
The SDK supports various authentication methods. You can configure authentication when initializing the SDK client.
API Key
Bearer Token
Basic Auth
OAuth 2.0 (Client Credentials)
OAuth 2.0 (Access Token)
Environment management
The SDK supports multiple environments (for example, production, staging) with different base URLs.
Error handling
The SDK raises custom exceptions for API errors, which include details about the error message, status code, and response body. You can catch these exceptions to handle errors in your application.
To handle errors, do the following:
To handle specific error codes, do the following:
Timeouts and retries
To set a global timeout, do the following:
To set a per-request timeout, do the following:
Config can be overridden per-request using requestConfig:
To configure retries, do the following:
Additional resources
Consider the following resources for using and customizing your TypeScript SDK:
- SDK documentation — Check the
documentation/directory in your SDK for detailed docs on services, models, and code snippets. - Type definitions — All models and services are fully typed.
- Example usage — Refer to the
examples/directory for working code samples demonstrating how to use the SDK in different scenarios. - Dependencies — (vary by transport selection):
- fetch (built-in) or axios: HTTP client
- zod: Runtime validation (when response validation is enabled)