Use Rust SDKs generated in Postman

View as Markdown

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.

SDK structure

Generated Rust SDKs include the following key components:

  • Core client — src/lib.rs as the main entry point and src/client.rs for the SDK client implementation.
  • Service modules — Organized in src/services/ with individual modules for each service.
  • Data models — Type-safe structs and enums in src/models/ powered by Serde.
  • HTTP infrastructure — Located in src/http/ handling configuration, transport, request handling, and authentication.
  • Dependencies — Specified in 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:

your-sdk
README.md
Cargo.toml
.gitignore
LICENSE
.devcontainer
examples
src

Type safety with Serde

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.

Model serialization

Models automatically serialize to and from JSON using Serde. The SDK handles field renaming, optional fields, and nested structures:

1use your_sdk::models::Pet;
2use serde_json;
3
4// Create a model instance
5let pet = Pet {
6 name: "Fluffy".to_string(),
7 status: "available".to_string(),
8 id: Some(1),
9 category: None,
10};
11
12// Serialize to JSON
13let json = serde_json::to_string(&pet)?;
14// {"name":"Fluffy","status":"available","id":1}
15
16// Deserialize from JSON
17let pet: Pet = serde_json::from_str(&json)?;

Optional and nullable fields

The SDK distinguishes between Option<T> fields (can be omitted) and fields that can be explicitly null using custom serde annotations:

1use your_sdk::models::UpdateContainerGroup;
2
3// Only set fields are included in API requests
4let update = UpdateContainerGroup {
5 replicas: Some(2),
6 country_codes: Some(vec!["us".to_string(), "eu".to_string()]),
7 liveness_probe: None, // This field is omitted from the request
8 display_name: Some("Updated Container".to_string()),
9};

Error handling

The SDK uses Rust’s Result<T, E> type for comprehensive error handling. All API calls return a Result with detailed error information.

Example usage

Each generated SDK includes an examples/ directory with working projects demonstrating various SDK usage patterns.

The examples include the following features:

  • Async usage patterns with Tokio
  • SDK configuration with different authentication methods
  • Type-safe API calls with full IntelliSense support
  • Comprehensive error handling with custom error types
  • Best practices for production usage

examples/example.rs

Install the Rust SDK

You can add the generated SDK to your Rust project using Cargo, either from a published crate or as a local dependency.

Install using Cargo

To add the SDK as a dependency from crates.io, add it to your Cargo.toml:

1[dependencies]
2your-sdk = "1.0.0"
3tokio = { version = "1.0", features = ["full"] }

Then use it in your code:

Install from a local path

To use the SDK from a local directory, add it as a path dependency in your Cargo.toml:

1[dependencies]
2your-sdk = { path = "../path/to/your-sdk" }
3tokio = { version = "1.0", features = ["full"] }

Install from Git repository

To install from a Git repository, add it as a Git dependency:

1[dependencies]
2your-sdk = { git = "https://github.com/your-org/your-sdk", branch = "main" }
3tokio = { version = "1.0", features = ["full"] }

For a specific version or tag:

1[dependencies]
2your-sdk = { git = "https://github.com/your-org/your-sdk", tag = "v1.0.0" }

Publish

To publish your generated Rust SDK, see Publish Rust SDKs generated in Postman.