The Python server library for the App Store Server API, App Store Server Notifications, Retention Messaging API, and Advanced Commerce API. Also available in Swift, Node.js, and Java.
- Python 3.8+
pip install app-store-server-libraryTo use the App Store Server API or create promotional offer signatures, a signing key downloaded from App Store Connect is required. To obtain this key, you must have the Admin role. Go to Users and Access > Integrations > In-App Purchase. Here you can create and manage keys, as well as find your issuer ID. When using a key, you'll need the key ID and issuer ID as well.
Download and store the root certificates found in the Apple Root Certificates section of the Apple PKI site. Provide these certificates as an array to a SignedDataVerifier to allow verifying the signed data comes from Apple.
fromappstoreserverlibrary.api_clientimportAppStoreServerAPIClient, APIExceptionfromappstoreserverlibrary.models.EnvironmentimportEnvironmentprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implementation will varykey_id="ABCDEFGHIJ"issuer_id="99b16628-15e4-4668-972b-eeff55eeff55"bundle_id="com.example"environment=Environment.SANDBOXclient=AppStoreServerAPIClient(private_key, key_id, issuer_id, bundle_id, environment)
try: response=client.request_test_notification()
print(response)
exceptAPIExceptionase:
print(e)fromappstoreserverlibrary.models.EnvironmentimportEnvironmentfromappstoreserverlibrary.signed_data_verifierimportVerificationException, SignedDataVerifierroot_certificates=load_root_certificates()
enable_online_checks=Truebundle_id="com.example"environment=Environment.SANDBOXapp_apple_id=None# appAppleId must be provided for the Production environmentsigned_data_verifier=SignedDataVerifier(root_certificates, enable_online_checks, environment, bundle_id, app_apple_id)
try: signed_notification="ey.."payload=signed_data_verifier.verify_and_decode_notification(signed_notification)
print(payload)
exceptVerificationExceptionase:
print(e)fromappstoreserverlibrary.api_clientimportAppStoreServerAPIClient, APIException, GetTransactionHistoryVersionfromappstoreserverlibrary.models.EnvironmentimportEnvironmentfromappstoreserverlibrary.receipt_utilityimportReceiptUtilityfromappstoreserverlibrary.models.HistoryResponseimportHistoryResponsefromappstoreserverlibrary.models.TransactionHistoryRequestimportTransactionHistoryRequest, ProductType, Orderprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implementation will varykey_id="ABCDEFGHIJ"issuer_id="99b16628-15e4-4668-972b-eeff55eeff55"bundle_id="com.example"environment=Environment.SANDBOXclient=AppStoreServerAPIClient(private_key, key_id, issuer_id, bundle_id, environment)
receipt_util=ReceiptUtility()
app_receipt="MI.."try: transaction_id=receipt_util.extract_transaction_id_from_app_receipt(app_receipt)
iftransaction_id!=None:
transactions= []
response: HistoryResponse=Nonerequest: TransactionHistoryRequest=TransactionHistoryRequest(
sort=Order.ASCENDING,
revoked=False,
productTypes=[ProductType.AUTO_RENEWABLE]
)
whileresponse==Noneorresponse.hasMore:
revision=response.revisionifresponse!=NoneelseNoneresponse=client.get_transaction_history(transaction_id, revision, request, GetTransactionHistoryVersion.V2)
fortransactioninresponse.signedTransactions:
transactions.append(transaction)
print(transactions)
exceptAPIExceptionase:
print(e)fromappstoreserverlibrary.promotional_offerimportPromotionalOfferSignatureCreatorimporttimeprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implementation will varykey_id="ABCDEFGHIJ"bundle_id="com.example"promotion_code_signature_generator=PromotionalOfferSignatureCreator(private_key, key_id, bundle_id)
product_id="<product_id>"subscription_offer_id="<subscription_offer_id>"application_username="<application_username>"nonce="<nonce>"timestamp=round(time.time()*1000)
base64_encoded_signature=promotion_code_signature_generator.create_signature(product_id, subscription_offer_id, application_username, nonce, timestamp)Include the optional async dependency
pip install app-store-server-library[async]fromappstoreserverlibrary.api_clientimportAsyncAppStoreServerAPIClient, APIExceptionfromappstoreserverlibrary.models.EnvironmentimportEnvironmentprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implementation will varykey_id="ABCDEFGHIJ"issuer_id="99b16628-15e4-4668-972b-eeff55eeff55"bundle_id="com.example"environment=Environment.SANDBOXclient=AsyncAppStoreServerAPIClient(private_key, key_id, issuer_id, bundle_id, environment)
try: response=awaitclient.request_test_notification()
print(response)
exceptAPIExceptionase:
print(e)
# Once client use is finishedawaitclient.async_close()Only the latest major version of the library will receive updates, including security updates. Therefore, it is recommended to update to new major versions.