App Store Server Library
The App Store Server Library is an official Python SDK provided by Apple for interacting with the App Store Server API. It simplifies backend tasks such as validating in-app purchases, retrieving transaction history, managing subscriptions, and checking the status of app purchases. Version 3.0.0 is the latest major release, aligning with the App Store Server API V2, and is actively maintained with updates typically following Apple's API evolution.
Warnings
- breaking Apple's App Store Server API deprecated the `verify_receipt` endpoint. The App Store Server Library reflects this, and its `AppStoreServerAPIClient` no longer provides a `verify_receipt` method. For individual transactions, use `get_transaction_info` or `get_all_transaction_info`. For broader purchase data, use `get_transaction_history` or `get_status_of_subscriptions`.
- breaking The `AppStoreServerAPIClient` constructor changed significantly in version 2.0.0 (and is retained in 3.0.0). The `private_key` parameter was renamed to `signing_key`, and the parameter order changed. Ensure you are providing the correct arguments: `(signing_key, key_id, issuer_id, bundle_id, environment)`.
- gotcha Always ensure the `Environment` passed to the `AppStoreServerAPIClient` (e.g., `Environment.SANDBOX` or `Environment.PRODUCTION`) matches the environment where your transactions occurred. Using `SANDBOX` for a production `transaction_id` or vice-versa will result in verification failures (e.g., HTTP 404 Not Found if transaction ID is not found in the specified environment).
- gotcha Incorrectly configured `signing_key`, `key_id`, `issuer_id`, or `bundle_id` will lead to authentication errors (e.g., HTTP 401 Unauthorized or 403 Forbidden). The `signing_key` must be the *content* of the private key (`.p8` file), not a file path. Ensure your bundle ID matches the app associated with the credentials.
Install
-
pip install app-store-server-library
Imports
- AppStoreServerAPIClient
from appstoreserverlibrary import AppStoreServerAPIClient
- Environment
from appstoreserverlibrary import Environment
Quickstart
import os
from appstoreserverlibrary import AppStoreServerAPIClient, Environment
# Ensure these environment variables are set for sandbox or production
SIGNING_KEY = os.environ.get("APP_STORE_SIGNING_KEY", "")
KEY_ID = os.environ.get("APP_STORE_KEY_ID", "")
ISSUER_ID = os.environ.get("APP_STORE_ISSUER_ID", "")
BUNDLE_ID = os.environ.get("APP_STORE_BUNDLE_ID", "")
# Use SANDBOX for testing, PRODUCTION for live apps
ENVIRONMENT = Environment.SANDBOX # or Environment.PRODUCTION
if not all([SIGNING_KEY, KEY_ID, ISSUER_ID, BUNDLE_ID]):
print("Please set APP_STORE_SIGNING_KEY, APP_STORE_KEY_ID, APP_STORE_ISSUER_ID, and APP_STORE_BUNDLE_ID environment variables.")
else:
try:
client = AppStoreServerAPIClient(
signing_key=SIGNING_KEY,
key_id=KEY_ID,
issuer_id=ISSUER_ID,
bundle_id=BUNDLE_ID,
environment=ENVIRONMENT
)
print("AppStoreServerAPIClient initialized successfully.")
# Example: Get subscription status for an original transaction ID
# For this to run, replace 'YOUR_ORIGINAL_TRANSACTION_ID' with a real one.
original_transaction_id = os.environ.get("APP_STORE_ORIGINAL_TRANSACTION_ID", "")
if original_transaction_id:
print(f"Fetching subscription status for: {original_transaction_id}")
try:
status_response = client.get_status_of_subscriptions(original_transaction_id)
print(f"Subscription Status: {status_response.to_json()}")
except Exception as e:
print(f"Error fetching subscription status: {e}")
else:
print("Set APP_STORE_ORIGINAL_TRANSACTION_ID env var to run subscription status example.")
except Exception as e:
print(f"Error initializing AppStoreServerAPIClient or making API call: {e}")