App Store Server Library

raw JSON →
3.0.0 verified Sun Apr 12 auth: no python

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.

pip install app-store-server-library
error ModuleNotFoundError: No module named 'appstoreserverlibrary'
cause This error occurs when the 'appstoreserverlibrary' package is not installed in your Python environment.
fix
Install the package using pip: 'pip install app-store-server-library'.
error ImportError: cannot import name 'AppStoreServerAPIClient' from 'appstoreserverlibrary'
cause This error occurs when attempting to import 'AppStoreServerAPIClient' from 'appstoreserverlibrary' without the correct module structure.
fix
Ensure the import statement is correct: 'from appstoreserverlibrary.api_client import AppStoreServerAPIClient'.
error APIException: INVALID_APP_IDENTIFIER
cause This error indicates that the app identifier provided in the request is invalid.
fix
Verify that the 'bundle_id' parameter matches the app's identifier in App Store Connect.
error APIException: INVALID_TRANSACTION_ID
cause This error occurs when the transaction identifier provided is invalid.
fix
Ensure that the 'transaction_id' parameter corresponds to a valid transaction in the App Store.
error APIException: ACCOUNT_NOT_FOUND
cause This error indicates that the App Store account associated with the request wasn't found.
fix
Confirm that the account information provided is correct and corresponds to an existing App Store account.
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`.
fix Migrate code to use `client.get_transaction_info(transaction_id)` or other relevant V2 endpoints provided by the library.
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)`.
fix Update client initialization to use `signing_key` and verify parameter order according to the documentation for versions 2.0.0 and above.
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).
fix Dynamically select the environment based on your application's deployment context or the source of the transaction ID (e.g., check `is_upgraded_from_android_in_app_billing` or other cues).
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.
fix Double-check all four credential values against your App Store Connect account (Users and Access -> Keys; ensure the key is active). Load the private key content correctly, for example, by reading the `.p8` file into a string.

Initializes the AppStoreServerAPIClient with credentials from environment variables and demonstrates fetching subscription status using a placeholder original transaction ID. Remember to configure your App Store Connect API Key, Issuer ID, and Bundle ID.

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}")