storekit-rs
Safe Rust bindings for Apple's StoreKit framework on macOS.
Status: v0.5.0. The
async_apimodule, gated on--features async, wrapsStoreKit2's Swift async APIs as RustFutures viadoom_fish_utils::completion.
Installation
[]
= "0.5"
The package is storekit-rs; the library is imported as storekit. It runs on macOS 12 or later, and APIs that need a newer macOS return StoreKitError::NotSupported on older systems. Building compiles a Swift bridge, which needs Xcode with the macOS 26.4 SDK or newer.
Build a macOS binary that uses this crate with a deployment target of macOS 12.0 or later. The
Swift bridge uses Swift concurrency. Rust builds binaries for macOS 11.0 on Apple silicon and for
macOS 10.12 on x86_64 by default. With these targets, the binary loads the Swift runtime
through @rpath and stops at launch with an error such as
Library not loaded: @rpath/libswift_Concurrency.dylib. Set MACOSX_DEPLOYMENT_TARGET=12.0 in
the environment, or set it for the project in .cargo/config.toml:
[]
= "12.0"
Cargo does not link the binary again when only this variable changes, so run cargo clean
after you set it.
Quick start
use *;
Highlights
Product::products_for(...),Product::purchase(...),Product::purchase_in_window(...),Product::latest_transaction(), andProduct::current_entitlements()- Product formatting/localization helpers expose
StoreKitformat styles,SubscriptionPeriodconvenience constructors, and enum/unitlocalized_description()helpers PurchaseOptioncovers quantity, app-account tokens, custom payloads, promotional offers, win-back offers, and storefront-change policiesTransactionexposesall,current_entitlements,updates,unfinished, filtered product streams,latest_for,current_entitlement_for, verification, finishing, refund helpers, and advanced-commerce infoSubscriptionInfo::status_for(...),status_for_transaction(...),SubscriptionStatus::updates(), andSubscriptionStatus::all()surface renewal state plus status streamsPurchaseIntent::intents()andExternalPurchase{,Link,CustomLink}cover the newerStoreKitpurchase-intent and regulatory-link familiesAppStorenow includes merchandising presentation, age-rating lookups, and advanced-commerce product purchase supportStoreKitError::typed()recovers typedStoreKit, purchase, refund-request, and invalid-request framework errorsStorefront::current()andStorefront::updates()wrap theStoreKitstorefront APIsAppTransaction::shared()andAppTransaction::refresh()expose app-level verification resultsReceiptValidatorreads the local app receipt and decodes JWS payloads without publishing or mutating store stateStoreContext::current()summarizes bundle, receipt, payment, and device-verification context for headless tooling
Verifying transactions
StoreKit returns every transaction, subscription status and app transaction together with the result of its JWS signature check. Read payloads through the checked accessors before granting access:
use Transaction;
payload_value()andinto_payload_value()return the payload only whenStoreKitverified it, andStoreKitError::Verificationotherwise, like Swift's throwingpayloadValue.unverified_payload()andinto_unverified_payload()skip the check, likeunsafePayloadValue. Don't grant entitlements from them.Transaction::finish()refuses to finish a transaction that failed verification.
Threading and timeouts
- Synchronous calls block the calling thread until
StoreKitanswers. Calls that don't present UI give up after 30 seconds: theStoreKittask is cancelled andStoreKitError::TimedOutis returned. - Calls that present
StoreKitUI run on the main actor:Product::purchase,Product::purchase_in_window,AppStore::present_merchandising,AdvancedCommerceProduct::purchase_in_window,AppStore::present_offer_code_redeem_sheet,Refund::begin_for_transaction_idandTransaction::begin_refund_request.- On the main thread they return
StoreKitError::NotSupportedright away, because the main actor could never run while they waited. - On any other thread they need the main thread to be running its run loop (for example
NSApplication.run). If the main actor doesn't start the work within 10 seconds, they returnTimedOutand nothing is presented. Once the main actor has started it they wait up to 10 minutes, then cancel it and returnTimedOut; a purchase that still completes afterwards is delivered throughTransaction::updates().
- On the main thread they return
AppStore::request_review()is synchronous inStoreKitand runs inline when called on the main thread.ExternalPurchase::present_notice_sheet,ExternalPurchaseLink::openandopen_url, andExternalPurchaseCustomLink::show_noticeletStoreKitpresent its own sheet and use the 30-second limit.- To start a purchase or merchandising flow from the main thread without blocking it, enable the
asyncfeature and useAsyncPurchase::buy,AsyncPurchase::buy_in_window,AsyncAppStore::present_merchandisingorAsyncAppStore::request_review. The futures areSend, so they can be spawned onto any executor; don't block the main thread on them. - Stream
next()waits until the next value arrives or the sequence ends.next_timeout()returnsStoreKitError::TimedOutwhen nothing arrives in time, and the stream stays open. Dropping a stream cancels itsStoreKitlistener. Keep aTransaction::updates()stream open for the life of the app, typically on its own thread, so renewals, refunds and Ask to Buy approvals aren't missed. Transaction,VerificationResult<Transaction>andPurchaseResultareSend + Sync, so a purchase made on a worker thread can be handed back to the UI thread.
Examples
The crate ships with numbered examples for each logical area:
01_product_lookup02_transaction_stream03_app_store_context04_storefront_current05_subscription_types06_subscription_info_status07_refund_request08_receipt_validator09_message_support10_app_transaction11_store_context12_renewal_info13_renewal_state14_purchase_option15_verification_result16_purchase_intent17_external_purchase18_advanced_commerce19_typed_errors20_async_products(requires--features async)21_async_app_transaction(requires--features async)22_async_storefront(requires--features async)
Run them all with:
for; do ; done
Async API
Enable the async feature to access StoreKit 2's async Swift APIs as standard Rust Futures:
[]
= { = "0.5", = ["async"] }
= "0.3" # or any async runtime
use AsyncProducts;
The following async types are available: AsyncProducts, AsyncPurchase, AsyncAppStore, AsyncAppTransaction and AsyncStorefront.
Notes
StoreKit.Messageis unavailable on macOS, so the message module reportsNotSupportedthere.AppStore::show_manage_subscriptions()andAsyncAppStore::show_manage_subscriptions()always returnNotSupported:AppStore.showManageSubscriptions(in:)is scene-based and isn't part of the macOSStoreKitSDK.- Refund, review, and offer-code presentation helpers use the key window's
NSViewControllerand returnNotSupportedwhen there is none. Without a running main run loop they returnTimedOutafter 10 seconds (see Threading and timeouts). - Window-based purchase and merchandising APIs accept caller-owned
NSWindowHandlevalues when the host app has anAppKitwindow to lend toStoreKit. The window is retained for the duration of the flow, so it only has to be alive when the call is made. - The crate does not publish or call
cargo publish; release tagging is separate from drip-publisher rollout.
License
Licensed under either of Apache-2.0 or MIT at your option.