Command: purchase
Relevant source files
The following files were used as context for generating this wiki page:
The purchase command is a core component of the IPATool CLI, designed to obtain a license for a specific iOS application from the Apple App Store. This process is a prerequisite for downloading app packages (.ipa files) for accounts that do not already own the application. It effectively "buys" the app (restricted to free apps) and associates it with the user's Apple ID.
The command operates by interacting with Apple's private iTunes Store APIs. It handles authentication state, performs app lookups via bundle identifiers, and manages the transaction flow, including fallback mechanisms for different pricing parameters like those used for Apple Arcade.
Sources: cmd/purchase.go:16-20, README.md:92-95, pkg/appstore/appstore_purchase.go:34-52
Execution Flow and Retry Logic
The purchase command implementation utilizes a retry strategy to handle transient authentication issues, specifically expired password tokens. When the command is executed, it first retrieves the cached account information and attempts the purchase. If the store returns an error indicating the session has expired, the tool attempts to re-log the user in before retrying the purchase.
The logic is encapsulated within a retry.Do block that allows for up to 2 attempts if a ErrPasswordTokenExpired is encountered.
Sources: cmd/purchase.go:25-70, pkg/appstore/appstore_purchase.go:12-14
Core Purchase Logic
The underlying logic for the purchase is handled by the appstore package. It performs several validation steps and specialized requests to Apple's servers.
Validation and Constraints
IPATool currently only supports obtaining licenses for free applications. If an app's price is greater than 0, the process will terminate with an error. Additionally, the system requires the machine's MAC address to generate a unique GUID for the request.
Sources: pkg/appstore/appstore_purchase.go:30-41
Pricing Parameters and Fallbacks
The purchase logic attempts to use standard App Store pricing parameters first. If the item is reported as "temporarily unavailable," the system automatically retries the request using Apple Arcade pricing parameters (GAME).
| Parameter Constant | Internal Value | Description |
|---|---|---|
PricingParameterAppStore | STDQ | Standard App Store purchase parameter |
PricingParameterAppleArcade | GAME | Fallback parameter for Arcade or specific subscription items |
Sources: pkg/appstore/appstore_purchase.go:44-55, pkg/appstore/appstore_purchase_test.go:250-280
Request Architecture
The purchase request is sent as an application/x-apple-plist POST request to Apple's MZFinance servers. The payload contains specific keys required by the Store API to identify the app and the intent.
API Request Details
The purchaseRequest function constructs the HTTP request with the following metadata and payload:
Headers
- iCloud-DSID / X-Dsid: The user's Directory Services ID.
- X-Apple-Store-Front: The storefront identifier (e.g.,
143441for the US). - X-Token: The user's password token obtained during login.
Sources: pkg/appstore/appstore_purchase.go:102-108
Payload Attributes
The XML payload includes several hardcoded flags to simulate a standard "buy" request:
- salableAdamId: The unique numerical ID of the app.
- appExtVrsId: Set to "0" for initial purchase.
- buyWithoutAuthorization: Set to "true".
- price: Set to "0" (enforcing the free-only constraint).
Sources: pkg/appstore/appstore_purchase.go:111-124
Error Handling
The command distinguishes between several specific error types returned by the App Store API:
| Error Type | Description |
|---|---|
ErrPasswordTokenExpired | The session token is no longer valid; requires re-authentication. |
ErrSubscriptionRequired | The item requires an active subscription (e.g. Apple Arcade). |
ErrTemporarilyUnavailable | The API returned a temporary failure; triggers the Apple Arcade fallback. |
InternalServerError (500) | Often interpreted by the tool as "license already exists" if returned during a purchase flow. |
Sources: pkg/appstore/appstore_purchase.go:11-15, pkg/appstore/appstore_purchase.go:73-86
Usage and Configuration
The command is accessed via the purchase subcommand. It requires a bundle identifier to target the specific app.
# Example usage
ipatool purchase --bundle-identifier com.example.app
| Flag | Shorthand | Description | Required |
|---|---|---|---|
--bundle-identifier | -b | The iOS app bundle ID (e.g., com.google.chrome) | Yes |
--format | Sets output format: text or json | No | |
--verbose | Enables detailed logging and stack traces | No |
Sources: cmd/purchase.go:76-78, cmd/root.go:43-52
Summary
The purchase command provides a critical bridge in the IPATool workflow, ensuring that the user's Apple ID has the legal right (license) to download an application. It abstracts the complexities of Apple's XML-based private APIs, manages session persistence through automatic re-login, and implements fallback logic to maximize the success rate of acquiring licenses for various types of free content on the App Store.