This guide provides instructions for using Rust 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.
The Postman SDK Generator creates Rust SDKs with type-safe models, service modules for API endpoints, comprehensive error handling with Result<T, E>, async/await support using Tokio, and extensive documentation to help you integrate with your API quickly and efficiently. Models leverage serde for JSON serialization/deserialization and provide compile-time type safety with Rust’s powerful type system.
Generated Rust SDKs include the following key components:
src/lib.rs as the main entry point and src/client.rs for the SDK client implementation.src/services/ with individual modules for each service.src/models/ powered by serde.src/http/ handling configuration, transport, request handling, and authentication.Cargo.toml (reqwest, serde, tokio for async support).
The directory structure includes documentation files, example implementations, and configuration for both development and production environments.The following is an example of the typical structure of a generated Rust SDK:
Generated models leverage serde for serialization and Rust’s type system for compile-time safety. All API interactions are validated at compile time, eliminating runtime type errors.
Models automatically serialize to and from JSON using serde. The SDK handles field renaming, optional fields, and nested structures:
The SDK distinguishes between Option<T> fields (can be omitted) and fields that can be explicitly null using custom serde annotations:
The SDK uses Rust’s Result<T, E> type for comprehensive error handling. All API calls return a Result with detailed error information.
Each generated SDK includes an examples/ directory with working projects demonstrating various SDK usage patterns.
The examples include the following features:
examples/example.rs
You can add the generated SDK to your Rust project using Cargo, either from a published crate or as a local dependency.
To add the SDK as a dependency from crates.io, add it to your Cargo.toml:
Then use it in your code:
To use the SDK from a local directory, add it as a path dependency in your Cargo.toml:
To install from a Git repository, add it as a Git dependency:
For a specific version or tag:
To share your generated Rust SDK with the community, you can publish it to crates.io. This allows other developers to easily install it using Cargo. Follow the steps below to publish your SDK to crates.io.
To publish your SDK to crates.io, you need the following:
Ensure your SDK’s Cargo.toml is properly configured for publication:
To publish your SDK to crates.io, do the following:
Create crates.io account and get API token:
Login to crates.io using Cargo:
Verify your package builds correctly:
Test the package build:
Publish to crates.io:
Verify publication:
Use cargo install your-sdk only if your crate also publishes a binary target.
To publish a new version of your SDK:
Update the version in Cargo.toml:
Update dependencies if needed and test:
Publish the new version:
Use the following best practices when publishing your Rust SDK to crates.io:
MAJOR.MINOR.PATCH).cargo doc.cargo test.#[non_exhaustive] on enums and structs that might grow.thiserror for error handling.For production SDKs, consider these additional configurations:
This ensures that docs.rs builds your documentation with all features enabled and proper conditional compilation attributes.