Basic Usage
Authentication
Api key and JWT token (Access token) from our identity provider are required to be able to access the API.
info
Most languages have libraries to get access token, more information about Auth0 token endpoint can be found here
Getting Access token manually
export CLIENT_ID='oidc_client_id'
export CLIENT_SECRET='oidc_client_secret'
curl -X POST https://auth.geniussports.com/oauth/token -H "Content-Type: application/x-www-form-urlencoded" --data grant_type=client_credentials --data client_id=$CLIENT_ID --data client_secret=$CLIENT_SECRET --data audience=https://api.geniussports.com
Sources
More information on available sources can be found here
SportIds
Currently supported sports:
- American Football ->
17
Integrating with Ably
- JavaScript
npm init
npm install ably --save
- Copy over the code snippet below
- Add missing secrets to the script
node index.js
var Ably = require('ably');
var https = require('https');
const clientId = '';
const clientSecret = '';
const audience = 'https://api.geniussports.com';
const sportId = 17;
const fixtureId = 8090594;
const source = 'GeniusPremium';
async function getAuthResponse(clientId, clientSecret, scopes) {
const payload = 'grant_type=client_credentials&client_id=' + clientId + '&client_secret=' + clientSecret + '&audience=' + audience;
const options = {
hostname: 'auth.geniussports.com',
port: 443,
path: '/oauth/token',
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
return new Promise(resolve => {
const request = https.request(options, response => {
let responseData = '';
response.on('data', chunk => responseData += chunk);
response.on('end', () => {
resolve({
statusCode: response.statusCode || 500,
body: JSON.parse(responseData)
});
});
});
request.on('error', err => reject(err));
request.write(payload);
request.end();
});
}
async function getAblyMetadata(accessToken, sportId, fixtureId, source) {
const options = {
hostname: 'statistics.api.geniussports.com',
port: 443,
path: '/v2/sports/' + sportId + '/fixtures/' + fixtureId + '/liveaccess?source=' + source,
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + accessToken,
}
};
return new Promise(resolve => {
const request = https.request(options, response => {
let responseData = '';
response.on('data', chunk => responseData += chunk);
response.on('end', () => {
resolve({
statusCode: response.statusCode || 500,
body: JSON.parse(responseData)
});
});
});
request.on('error', err => reject(err));
request.end();
});
}
(async () => {
let authTokens = await getAuthResponse(clientId, clientSecret, scopes);
if (authTokens.statusCode != 200) {
throw new Error("something went wrong");
}
let ablyMetadata = await getAblyMetadata(authTokens.body.accessToken, sportId, fixtureId, source);
if (ablyMetadata.statusCode != 200) {
throw new Error("something went wrong");
}
var client = new Ably.Realtime({
token: ablyMetadata.body.accessToken,
environment: "geniussports"
});
client.connection.on(function(stateChange) {
console.log('New connection state is ' + stateChange.current);
});
client.connection.on('connected', function() {
let channel = client.channels.get(ablyMetadata.body.channelName);
channel.subscribe(data => console.log(data));
});
client.connection.on('failed', function(err) {
console.log(err);
});
})();
We recommend using authCallback
property in Ably.Realtime
constructor to automatically refresh the token for Ably connection.
let channelName = '';
var client = new Ably.Realtime({
authCallback: (tokenParams, callback) => { getAuthResponse(clientId, clientSecret, scopes).then(data => {
getAblyMetadata(data.body.accessToken, sportId, fixtureId, source).then(metadata => {
channelName = metadata.body.channelName;
callback(null, metadata.body.accessToken);
})
})},
environment: "geniussports"
});
client.connection.on('connected', function() {
let channel = client.channels.get(channelName);
channel.subscribe(data => console.log(data));
});