Apple App Store Server API Client

0.17.3 · active · verified Sun Apr 19

This package provides a robust Node.js client for the Apple App Store Server API, simplifying interactions with Apple's services for in-app purchases and subscriptions. Currently stable at version 0.17.3, it receives frequent updates, often aligning with new releases and features of the underlying App Store Server API. Key features include automatic management of authentication tokens, comprehensive support for App Store Server Notifications V2, typed responses for enhanced developer experience, and helpers for decoding JWS (JSON Web Signature) items with built-in certificate validation against Apple's Certificate Authority. It distinguishes itself by abstracting away much of the complexity of the Apple API, including handling of transaction history, subscription status, and order lookup endpoints, while providing type-safe parameters for filtering and sorting.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the App Store Server API client and fetch a user's recent auto-renewable transaction history, including decoding and verifying the signed transaction data. It highlights the use of environment variables for sensitive API credentials.

import { AppStoreServerAPI, Environment, ProductTypeParameter, SortParameter } from 'app-store-server-api';

// Ensure these are loaded from secure environment variables in a production setup
const KEY = process.env.APPLE_PRIVATE_KEY ?? 
`-----BEGIN PRIVATE KEY-----
MHcCAQEEIPWH5lyoG7Wbzv71ntF6jNvFwwJLKYmPWN/KBD4qJfMcoAoGCCqGSM49
AwEHoUQDQgAEMOlUa/hmyAPU/RUBds6xzDO8QNrTFhFwzm8E4wxDnSAx8R9WOMnD
cVGdtnbLFIdLk8g4S7oAfV/gGILKuc+Vqw==
-----END PRIVATE KEY-----`;
const KEY_ID = process.env.APPLE_KEY_ID ?? "ABCD123456";
const ISSUER_ID = process.env.APPLE_ISSUER_ID ?? "91fa5999-7b54-4363-a2a8-265363fa6cbe";
const APP_BUNDLE_ID = process.env.APPLE_APP_BUNDLE_ID ?? "com.yourcompany.app";

const api = new AppStoreServerAPI(
  KEY, KEY_ID, ISSUER_ID, APP_BUNDLE_ID, Environment.Sandbox // Use Environment.Production for live apps
);

async function getRecentPurchases(originalTransactionId: string) {
  try {
    const response = await api.getTransactionHistory(originalTransactionId, {
      productType: ProductTypeParameter.AutoRenewable,
      sort: SortParameter.Descending,
      limit: 20 // Fetch up to 20 transactions
    });

    // Decoding verifies the signature and provides typed access to transaction data.
    const transactions = await api.decodeTransactions(response.signedTransactions);

    console.log(`Found ${transactions.length} transactions for ${originalTransactionId}:`);
    for (const transaction of transactions) {
      console.log(`  Transaction ID: ${transaction.transactionId}, Product ID: ${transaction.productId}, Purchase Date: ${new Date(transaction.purchaseDate).toISOString()}`);
      // Further processing with transaction data...
    }

    if (response.hasMore) {
      console.log('More transactions available. Consider fetching with response.revision for next page.');
    }
  } catch (error) {
    console.error('Error fetching transaction history:', error);
  }
}

// Example usage (replace with a real originalTransactionId from your system)
// In a real application, you'd get this from your database or client.
const exampleOriginalTransactionId = "123456789012345"; 
getRecentPurchases(exampleOriginalTransactionId);

view raw JSON →