'How to get the Units by day via API - App store connect

Is it possible to get the data from Units download by day or installed via API ? but the problem is hard to find the resources of documentation of it.

this image below is the data I want to have.

https://i.stack.imgur.com/P3NDF.png



Solution 1:[1]

It has several steps to archieve. First you have to follow 2 links here: Create keys: https://developer.apple.com/documentation/appstoreconnectapi/creating_api_keys_for_app_store_connect_api

Create and sign JWT Token https://developer.apple.com/documentation/appstoreconnectapi/generating_tokens_for_api_requests

These important keys to get is:

IssuerId
KeyId
VendorId
PrivateKey

If you are using Python, I would suggest using PyJWT to sign it

from datetime import datetime, timezone
import jwt

def sign_appstore_token(issuer_id, key_id, generated_private_key):
    bin_private_key = generated_private_key.encode()
    current_unix = int(datetime.now(tz=timezone.utc).timestamp())
    token = jwt.encode({
            "iss": issuer_id,
            "iat": current_unix,
            "exp": current_unix + 1000, 
            "aud": "appstoreconnect-v1",
    }, key= bin_private_key, algorithm= 'ES256', headers= {
        "alg": "ES256",
        "kid": key_id,
        "typ": "JWT"
    })
    return token

From generated token, continue following this link https://developer.apple.com/documentation/appstoreconnectapi/download_sales_and_trends_reports

To get the Units, reportType should be SALES. Also noticed that reportDate and frequency have to consistency each other, if you specify filter[frequency] = YEARLY, then filter[reportDate] = 2021 or filter[frequency] = MONTHLY, then filter[reportDate] = 2021-06. For more details, please refer to the above link

Sample query here:

https://api.appstoreconnect.apple.com/v1/salesReports?filter[frequency]=YEARLY&filter[reportDate]=2021&filter[reportSubType]=SUMMARY&filter[reportType]=SALES&filter[vendorNumber]=YOUR_VENDOR_ID
Headers: Authorization: Bearer YOUR_ABOVE_TOKEN

You will get binary response if it is success, represented for .gz file as well. Extract gz to get .txt schema deliminated by \t

Columns:

Provider    Provider Country    SKU Developer   Title   Version Product Type Identifier Units   Developer Proceeds  Begin Date  End Date    Customer Currency   Country Code    Currency of Proceeds    Apple Identifier    Customer Price  Promo Code  Parent Identifier   Subscription    Period  Category    CMB Device  Supported Platforms Proceeds Reason Preserved Pricing   Client  Order Type

Python script here returns file content as text, you can do your next step, pandas table, or to model, it is up to you

import requests
import gzip
def download_appstore_objects(token, vendor_id, frequency, reportDate):
    link = f'https://api.appstoreconnect.apple.com/v1/salesReports?filter[frequency]={frequency}&filter[reportDate]={reportDate}&filter[reportSubType]=SUMMARY&filter[reportType]=SALES&filter[vendorNumber]={vendor_id}'
    response = requests.get(link, headers= {'Authorization': f'Bearer {token}' })
    file_content = gzip.decompress(response.content).decode('utf-8')
    return file_content

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 T?n Nguyên