The Node.js server library for the App Store Server API, App Store Server Notifications, Retention Messaging API, and Advanced Commerce API. Also available in Swift, Python, and Java.
- Node 16+
# With NPM
npm install @apple/app-store-server-library --save
# With Yarn
yarn add @apple/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.
import{AppStoreServerAPIClient,Environment,SendTestNotificationResponse}from"@apple/app-store-server-library"constissuerId="99b16628-15e4-4668-972b-eeff55eeff55"constkeyId="ABCDEFGHIJ"constbundleId="com.example"constfilePath="/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"constencodedKey=readFile(filePath)// Specific implementation may varyconstenvironment=Environment.SANDBOXconstclient=newAppStoreServerAPIClient(encodedKey,keyId,issuerId,bundleId,environment)try{constresponse: SendTestNotificationResponse=awaitclient.requestTestNotification()console.log(response)}catch(e){console.error(e)}import{SignedDataVerifier}from"@apple/app-store-server-library"constbundleId="com.example"constappleRootCAs: Buffer[]=loadRootCAs()// Specific implementation may varyconstenableOnlineChecks=trueconstenvironment=Environment.SANDBOXconstappAppleId=undefined// appAppleId is required when the environment is Productionconstverifier=newSignedDataVerifier(appleRootCAs,enableOnlineChecks,environment,bundleId,appAppleId)constnotificationPayload="ey..."constverifiedNotification=awaitverifier.verifyAndDecodeNotification(notificationPayload)console.log(verifiedNotification)import{AppStoreServerAPIClient,Environment,GetTransactionHistoryVersion,ReceiptUtility,Order,ProductType,HistoryResponse,TransactionHistoryRequest}from"@apple/app-store-server-library"constissuerId="99b16628-15e4-4668-972b-eeff55eeff55"constkeyId="ABCDEFGHIJ"constbundleId="com.example"constfilePath="/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"constencodedKey=readFile(filePath)// Specific implementation may varyconstenvironment=Environment.SANDBOXconstclient=newAppStoreServerAPIClient(encodedKey,keyId,issuerId,bundleId,environment)constappReceipt="MI..."constreceiptUtil=newReceiptUtility()consttransactionId=receiptUtil.extractTransactionIdFromAppReceipt(appReceipt)if(transactionId!=null){consttransactionHistoryRequest: TransactionHistoryRequest={sort: Order.ASCENDING,revoked: false,productTypes: [ProductType.AUTO_RENEWABLE]}letresponse: HistoryResponse|null=nulllettransactions: string[]=[]do{constrevisionToken=response!==null&&response.revision!==null ? response.revision : nullresponse=awaitclient.getTransactionHistory(transactionId,revisionToken,transactionHistoryRequest,GetTransactionHistoryVersion.V2)if(response.signedTransactions){transactions=transactions.concat(response.signedTransactions)}}while(response.hasMore)console.log(transactions)}import{PromotionalOfferSignatureCreator}from"@apple/app-store-server-library"constkeyId="ABCDEFGHIJ"constbundleId="com.example"constfilePath="/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"constencodedKey=readFile(filePath)// Specific implementation may varyconstproductId="<product_id>"constsubscriptionOfferId="<subscription_offer_id>"constappAccountToken="<app_account_token>"constnonce="<nonce>"consttimestamp=Date.now()constsignatureCreator=newPromotionalOfferSignatureCreator(encodedKey,keyId,bundleId)constsignature=signatureCreator.createSignature(productId,subscriptionOfferId,appAccountToken,nonce,timestamp)console.log(signature)Only the latest major version of the library will receive updates, including security updates. Therefore, it is recommended to update to new major versions.