You are on page 1of 5

Token Generation API Connection with PowerBI

In addition to the API key, some Platts API datasets may require an access token to be included in every
API request. This token can be generated using your S&P Global Single Sign-On credentials via the Token
Generation API endpoint. It is a common use case, when setting up APIs in PowerBI, to want to connect
the APIs to a dynamic token API, to avoid the need to manually paste the token access into the API each
time the data is refreshed. This documentation aims to assist in this area.

The Platts Market Data API (v3) requires a token, as do a number of other APIs. The example included
calls the closing pricing history for Dated Brent (PCAAS00) from 1-Jan-2022 to 1-May-2022.

1. Get Data via Web to create a dataset query

▪ Go to Developer Portal and copy the URL from a successful call (or use another URL that works)
▪ Return to PowerBI, click on ‘Get Data’ and select ‘Advanced’ and input as follows:
o Paste the saved URL into ‘URL parts’ field
o ‘appkey’ in HTTP request header parameters and input your API Key into the associated
adjacent value field
o Click ‘Add Header’ and input ‘Authorization’ into the left hand field and ‘Bearer ‘ plus the
token into the right hand field
o Click ‘OK’
▪ When Access Web content window populates, allow it to default to ‘Anonymous’ (authentication
is included in the request header) and click ‘Connect’

▪ When Navigator window populates, click check mark next to datapoint and click ‘Load’
▪ Fields auto populate on right hand side for selection. This is an example for a Market Data API
history extract of Dated Brent (symbol PCAAS00)

2. Write a function to pull Token from Token API

▪ To write a function, right click on any dataset query in the Fields column and select “Edit Query”
▪ Select “New Source” and then “Blank Query”

▪ Input the following code in the blank query, including your username, domain name, password
and API key accordingly – see example below for the elements in red. For example
o user.name = john.jones
o domain = spglobal.com
o password = yourpassword
o appkey = your API key, in quotes

= () =>
let
body = "username=user.name%40domain&password=password",

Data=Json.Document(Web.Contents("https://api.platts.com/auth/api",[Headers=[appkey="appkey",
#"Content-Type"="application/x-www-form-urlencoded"],Content=Text.ToBinary(body)])),
access_token=Data[access_token]
in
access_token

▪ If done correctly, you should see your query change to a ‘function’, identified by ‘fx’. It will look
like this in the left hand side list of queries; here it’s renamed to ‘TokenQuery’:

Do not miss the “= () =>” feature above, as that allows PowerBI to recognize it as a function.
3. Put the function in the API

▪ Edit the Source of the dataset query created in step 1 (here it is called ‘PCAAS00 query’) – open it
by clicking on the query shown in the right hand side list then choose Advanced Editor
▪ Remove the token and replace by ‘&’ plus your query name. In the example this is TokenQuery so
what’s needed is Authorization="Bearer "&TokenQuery() .
▪ On invoking the dataset query, you will be asked to Edit Credentials. Proceed as before

▪ The data is returned

The Market Data API (v3) call now has a dynamically refreshable token inserted in the API request
header.

You might also like