Skip to content

Instantly share code, notes, and snippets.

@CharlieHess
Created July 11, 2019 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlieHess/3bd32892825e3e25f7a789efb4a5775c to your computer and use it in GitHub Desktop.
Save CharlieHess/3bd32892825e3e25f7a789efb4a5775c to your computer and use it in GitHub Desktop.
Proof of concept for making App Store Connect requests
/* tslint:disable: no-console */
import * as fs from 'fs-extra';
import { sign } from 'jsonwebtoken';
import { memoize, take } from 'lodash';
import fetch from 'node-fetch';
import * as path from 'path';
const KEY_ID = 'XXXXSECRET';
const ISSUER_ID = 'secret-xxxx-xxxx-xxxx-secret';
export interface AppStoreBuild {
type: 'builds';
id: string;
attributes: Object;
relationships: Object;
links: Object;
}
export interface AppStoreBuildResponse {
data: Array<AppStoreBuild>;
}
/**
* @note See https://developer.apple.com/documentation/appstoreconnectapi/generating_tokens_for_api_requests
*/
export const generateAppStoreJsonWebToken = memoize(() => {
const header = {
alg: 'ES256',
kid: KEY_ID,
typ: 'JWT'
};
const payload = {
iss: ISSUER_ID,
aud: 'appstoreconnect-v1'
};
const pathToPrivateKey = path.resolve(__dirname, 'apple-private-key.p8');
return sign(payload, fs.readFileSync(pathToPrivateKey), {
expiresIn: '20m',
header
});
});
export async function getAppStoreBuilds() {
const buildsResponse = await fetch('https://api.appstoreconnect.apple.com/v1/builds', {
method: 'GET',
headers: {
Authorization: `Bearer ${generateAppStoreJsonWebToken()}`
}
});
const { data } = await buildsResponse.json<AppStoreBuildResponse>();
return data;
}
export async function getAppInfo(id: string) {
const appInfoResponse = await fetch(`https://api.appstoreconnect.apple.com/v1/builds/${id}/app`, {
method: 'GET',
headers: {
Authorization: `Bearer ${generateAppStoreJsonWebToken()}`
}
});
const { data } = await appInfoResponse.json();
return data;
}
async function main() {
const builds = await getAppStoreBuilds();
take(builds, 10).forEach(async ({ id }) => {
const info = await getAppInfo(id);
console.log(`App Info (${id}):`, info);
});
}
// tslint:disable-next-line: no-floating-promises
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment