Is Shima Right for You?
Shima is a lightweight, high-performance Stripe API client library written in Rust. It is designed for developers who need a fast, type-safe, and minimal-dependency way to integrate Stripe payments into their Rust applications. With that being said, Shima is not suitable for all use cases. Here are some scenarios where Shima might be a good fit:
- You use Stripe for checkouts and customer management.
- You use Stripe for Subscriptions.
- You use Stripe for Webhooks regarding Subscriptions.
Benefits
- Shima compiles up to 10x faster than
async-stripe.
- Fast
- Type-safe
- Minimal dependencies
- Easy to use
Getting Started
Add shima to your Cargo.toml file:
cargo add shima
Usage
Generating a new shima client
let client = shima::Client::from_env();
let client = shima::Client::new("sk_test_123456...");
let client = shima::Client::new("sk_test_123456...").with_webhook_secret("whsec_123456...");
Creating a Stripe Customer
use shima::customer::{Customer, CreateCustomer};
async fn create_customer() -> Result<Customer, shima::Error> {
let client = shima::Client::from_env();
let mut customer = CreateCustomer::new("John Doe", "john@example.com");
customer.metadata.insert("user_id", "123456");
Customer::create(&client, customer).await
}
Purchasing Subscriptions / Checkout
use shima::checkout::{CheckoutSession, CreateCheckoutSession};
use shima::{CustomerId, PriceId, CancelUrl, SuccessUrl};
async fn create_checkout_session() -> Result<CheckoutSession, shima::Error> {
let client = shima::Client::from_env();
let mut session = CreateCheckoutSession::new_subscription(
CustomerId::try_from("cus_1234567")?,
PriceId::try_from("price_1234567")?,
SuccessUrl::from("https://example.com/success"),
CancelUrl::from("https://example.com/cancel"),
);
session.metadata.insert("user_id", "1");
CheckoutSession::create(&client, session).await
}
Manage Subscriptions / Customer Portal
use shima::billing::{CustomerPortalSession, CreateCustomerPortalSession};
use shima::{CustomerId, ReturnUrl};
async fn manage_subscription() -> Result<CustomerPortalSession, shima::Error> {
let client = shima::Client::from_env();
let customer = CustomerId::try_from("cus_123456")?;
let return_url = ReturnUrl::from("https://example.com");
let session = CreateCustomerPortalSession::new(customer, return_url);
CustomerPortalSession::create(&client, session).await
}
Webhooks
use shima::webhook::ShimaEvent;
fn listen_to_webhooks(headers: &http::HeaderMap, body: &str) -> Result<(), shima::Error> {
let client = shima::Client::from_env();
let listener = shima::webhook::Listener::new(client);
match listener.process(headers, body)? {
ShimaEvent::CheckoutSessionCompleted(event) => println!("Checkout session completed: {:?}", event),
ShimaEvent::InvoicePaymentFailed(event) => println!("Invoice payment failed: {:?}", event),
ShimaEvent::CustomerSubscriptionDeleted(event) => println!("Customer subscription deleted: {:?}", event),
ShimaEvent::Other(event) => println!("Other event: {:?}", event),
}
Ok(())
}