The Python server library for the App Store Server API and App Store Server Notifications
This software is currently in Beta testing. Therefore, it should only be used for testing purposes, like for the Sandbox environment. API signatures may change between releases and signature verification may receive security updates.
- Python 3.7+
pip install app-store-server-libraryfromappstoreserverlibrary.api_clientimportAppStoreServerAPIClient, APIExceptionfromappstoreserverlibrary.models.EnvironmentimportEnvironmentfromappstoreserverlibrary.models.SendTestNotificationResponseimportSendTestNotificationResponseprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implemenation 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.SANDBOXsigned_data_verifier=SignedDataVerifier(root_certificates, enable_online_checks, environment, bundle_id)
try: signed_notification="ey.."payload=signed_data_verifier.verify_and_decode_notification()
print(payload)
exceptVerificationExceptionase:
print(e)fromappstoreserverlibrary.api_clientimportAppStoreServerAPIClient, APIExceptionfromappstoreserverlibrary.models.EnvironmentimportEnvironmentfromappstoreserverlibrary.receipt_utilityimportReceiptUtilityfromappstoreserverlibrary.models.HistoryResponseimportHistoryResponsefromappstoreserverlibrary.models.TransactionHistoryRequestimportTransactionHistoryRequest, ProductType, Orderprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implemenation 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)
fortransactioninresponse.signedTransactions:
transactions.append(transaction)
print(transactions)
exceptAPIExceptionase:
print(e)fromappstoreserverlibrary.promotional_offerimportPromotionalOfferSignatureCreatorimporttimeprivate_key=read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # Implemenation 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)Only the latest major version of the library will receive updates, including security updates. Therefore, it is recommended to update to new major versions.