Sunday, August 30, 2020

Invoking AppStore Connect API via Command Line

Sometime back Apple had opened up the AppStore Connect API for its developers. The API is a bunch of RESTful service which can be used to customise and automate our workflows. It helps us automate tasks across developer tools, such as App Store Connect, Xcode, and Certificates, Identifiers & Profiles, to give us greater flexibility and increased efficiency in our workflows. We could use it for development, beta testing, managing app metadata, reporting etc.

It can be very helpful to invoke the AppStore Connect API via command line. I use it primarily to check the structure of the response object. Although the documentation of the AppStore Connect API is good, but there is nothing as good as seeing the response object with real values in it.

In this post we will see how can we invoke the AppStore Connect API from the command line.

How To Do It? 

To invoke the AppStore Connect API via command line we will follow these steps.

  • Create API Key from the App Store Connect Web Portal
  • Create JWT JSON Token for Accessing API using XCToken
  • Invoking the API from command line using curl.

Create API Key 

  • To generate an API Key you will need to login to iTunes Connect portal. 
  • Navigate to the "Users and Access" section

  • In there, navigate to the "Keys" tab
  • Choose the "Key Type" as "AppStore Connect API"
  • Make a note of the "Issuer Id" we will be using this later on to generate the JWT JSON Token.

  • Generate a New API Key, by clicking the "+" icon. This will open up a dialog which ask you to name the key and select the Access type. I am going to choose "Developer" for this post.

  • This will generate the API key, make a note of the generated Key Id we will be using it later to generated the JWT JSON Token.
  • iTunesConnect will give you an opportunity to download the generated key only once. So please make sure you download it and keep it safe.

  • This completes the steps necessary to generate an API Key.

Create JWT JSON Token

To Generate the JWT JSON Token we are going to use a command line utility called XCToken. It can generate on-demand JWT tokens forAppStore Connect API. To install the utility just run the following command.

gem install xctoken
To generate the JWT JSON Token this utility expects three environment variables.
  • ISSUER_ID = This is the issuer id which we have noted in the earlier step from the iTunes Connect page.
  • KEY_DIR = The full directory path where the API key was downloaded from iTunes Connect
  • KEY_ID = The key id of the newly generated API key.
Once these environment variables are set up the XCToken is ready to generate the JWT JSON Token. Here is the sample script that will generate the token.

export KEY_DIR=~/Downloads/
export KEY_ID=ABCD1234
xctoken  generate
This will spit out the token on the console, make a note of this token, we will be using it to invoke the AppStore Connect API.

Invoking the API

Now that we have generated a new JWT JSON Token, we are ready to invoke the AppStore Connect API. Here you can find various endpoints and their documentation. For e.g. to get a list of all your apps, use the following command.

curl --Header "Authorization: Bearer <GENERATED TOKEN>"
Thats about all we need to do to invoke the AppStore Connect API via command line!

Have some Fun!