App Store Server Library

3.0.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

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

view raw JSON →