Publish Rust SDKs manually

View as Markdown

This guide covers publishing Rust SDKs manually. For information on automating Rust SDK publishing, see Publish Rust SDKs automatically.

To share your generated Rust SDK with the community, you can publish it to crates.io. This enables other developers to easily install it using Cargo. Follow the steps below to publish your SDK to crates.io.

Prerequisites

To publish your SDK to crates.io, you need the following:

  • A crates.io account (create at crates.io).
  • Cargo installed with the Rust toolchain.
  • API token from crates.io.

Configure Cargo.toml

Ensure your SDK’s Cargo.toml is properly configured for publication:

1[package]
2name = "your-sdk"
3version = "1.0.0"
4description = "SDK for Your API"
5license = "MIT" # or license-file = "LICENSE"
6repository = "https://github.com/your-org/your-sdk"

Publish your Rust SDK to crates.io

To publish your SDK to crates.io, do the following:

  1. Create a crates.io account and get an API token:

    • Register at crates.io.
    • Go to Account Settings > API Tokens.
    • Create a new token with appropriate permissions.
  2. Sign in to crates.io using Cargo:

    $cargo login <your-api-token>
  3. Verify your package builds correctly:

    $# Check for common issues
    $cargo check
    $
    $# Run tests
    $cargo test
    $
    $# Build documentation
    $cargo doc
    $
    $# Verify package contents
    $cargo package --list
  4. Test the package build:

    $# This creates a .crate file without publishing
    $cargo package
  5. Publish to crates.io:

    $cargo publish
  6. Verify publication:

    $# In a separate test project, add your published crate as a dependency
    $cargo new verify-your-sdk
    $cd verify-your-sdk
    $cargo add your-sdk
    $
    $# Build the project to verify the crate resolves and compiles
    $cargo check
    $
    $# Or visit: https://crates.io/crates/your-sdk

    Use cargo install your-sdk only if your crate also publishes a binary target.

Publish updates

To publish a new version of your SDK:

  1. Update the version in Cargo.toml:

    1version = "1.0.1"
  2. Update dependencies if needed and test:

    $cargo update
    $cargo test
  3. Publish the new version:

    $cargo publish

Best practices for publishing Rust SDKs

Use the following best practices when publishing your Rust SDK to crates.io:

  • Follow semantic versioning (MAJOR.MINOR.PATCH).
  • Include comprehensive documentation with cargo doc.
  • Use appropriate keywords and categories for discoverability.
  • Test your package thoroughly with cargo test.
  • Consider using features for optional functionality.
  • Keep your API surface minimal and follow Rust naming conventions.
  • Use #[non_exhaustive] on enums and structs that might grow.
  • Provide good error messages and use thiserror for error handling.

Advanced configuration

For production SDKs, consider these additional configurations:

Custom feature flags

1[features]
2default = ["reqwest-default"]
3reqwest-default = ["reqwest/default-tls"]
4reqwest-rustls = ["reqwest/rustls-tls"]
5blocking = ["reqwest/blocking"]

Documentation settings

1[package.metadata.docs.rs]
2all-features = true
3rustdoc-args = ["--cfg", "docsrs"]

This ensures that docs.rs builds your documentation with all features enabled and proper conditional compilation attributes.