Generating a signature for promotional offers
Create a signature to validate a promotional offer using your private key.
Overview
Before you can create a signature on your server, you need to complete the one-time setup to generate a private key in App Store Connect, as Setting up promotional offers describes. Always use a secure connection when sending data, including the signature, between your app and server. For more information on ensuring your data’s security, see Preventing Insecure Network Connections.
To create the signature, you use parameters that identify the product and the offer, parameters your server generates, and your private key. To generate the signature, you combine the required parameters, then sign and encode the resulting string.
Combine the parameters
In the first step of generating the signature, you need the following parameters, most of which you also supply for SKPaymentDiscount:
appBundleIDThe app bundle identifier.
keyIdentifierA string that identifies the private key you use to generate the signature. You can find this identifier in App Store Connect Users and Access > Keys in the Key ID column for the subscription key you generate.
productIdentifierThe subscription product identifier, productIdentifier. The app can provide this value.
offerIdentifierThe subscription discount identifier, identifier. The app can provide this value.
applicationUsername or appAccountTokenAn optional string value that you define; may be an empty string. If your app uses applicationUsername, provide
applicationUsername. If your app uses appAccountToken, provideappAccountToken. The string representation of theappAccountTokenmust be lowercase.nonceA one-time
UUIDvalue that your server generates. Generate a newnoncefor every signature. The string representation of thenonceyou use in the signature must be lowercase.timestampA timestamp your server generates in UNIX time format, in milliseconds. The timestamp keeps the offer active for 24 hours.
Combine the parameters into a UTF-8 string with an invisible separator ('\u2063') between them in the same order as the following example:
appBundleId + '\u2063' + keyIdentifier + '\u2063' + productIdentifier + '\u2063' + offerIdentifier + '\u2063' + appAccountToken + '\u2063' + nonce + '\u2063' + timestampIf you provide applicationUsername instead of appAccountToken, replace it accordingly in the UTF-8 string above.
Sign the combined string
Sign the combined UTF-8 string with the following key and algorithm:
Your PKCS #8 private key (downloaded from App Store Connect) that corresponds to the
keyIdentifierin the UTF-8 stringThe Elliptic Curve Digital Signature Algorithm (ECDSA) with a SHA-256 hash
The result is a Digital Encoding Rules (DER)-formatted binary value, which is the signature.
Validate locally and encode the signature
Consider validating your signatures locally to ensure your signing process works correctly. You can create a public key derivative of your private key to test against. One way to create this key is by running the openSSL command from the terminal app, as the example below shows:
openssl ec -in {downloaded_private_key} -pubout -out public_key.pemUse Base64 encoding for the binary signature you generated to obtain the final signature string to send to the App Store for validation. The signature string resembles the following:
MEQCIEQlmZRNfYzKBSE8QnhLTIHZZZWCFgZpRqRxHss65KoFAiAJgJKjdrWdkLUOCCjuEx2RmFS7daRzSVZRVZ8RyMyUXg==Respond to the request
Respond to the app’s request for the signature over a secure connection, providing the encoded signature string, the nonce, the timestamp, and the keyIdentifier. Note that each payload, signature, and nonce is only valid for one buy request, even if the buy fails.
See Create a Signature for information about the app’s request and how it uses the signature.