Authentication
The API uses the OAuth2.0 protocol for authentication, and supports the use of the Authorization Client Credentials flow.
The following example demonstrates how to use the Authorization Client Credentials flow to obtain an access token and send an authenticated request to the API. It is a minimal example; most languages have OAuth2 libraries that can handle the client credentials flow for you.
Managing OAuth2 clients
Using the Portal under the "My Organization" section, in the "API Access" area at the bottom, you can create and manage your OAuth2 clients and the Client ID and Client Secret.
Click the "+Add" button to create a new client. Give it a descriptive name and click "Create client" to obtain the Client ID and Client Secret.
N.B. remember to copy the client secret, this is the only chance you get to see it.
Code example
- Python
- JavaScript
import os
import requests
# 1. Exchange your client credentials for an access token
token_response = requests.post(
"https://auth.tideform.io/oauth2/token",
data={
"grant_type": "client_credentials",
"scope": "api",
"client_id": os.getenv("CLIENT_ID", "some-client-id"),
"client_secret": os.getenv("CLIENT_SECRET", "some-client-secret"),
},
)
access_token = token_response.json()["access_token"]
# 2. Send the access token as a Bearer token with every API request
response = requests.post(
"https://api.tideform.io/graphql",
json={
"query": """
query spotPrice {
latestSpotPrices(input: { portIds: ["sgsin"], fuelGradeIds: ["mgo"] }) {
port { id name }
fuelGrade { id description }
latestPrices { publishedDate price band68Lower band68Upper }
}
}
"""
},
headers={"Authorization": f"Bearer {access_token}"},
)
data = response.json()["data"]
// 1. Exchange your client credentials for an access token
const tokenResponse = await fetch("https://auth.tideform.io/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "client_credentials",
scope: "api",
client_id: process.env.CLIENT_ID || "some-client-id",
client_secret: process.env.CLIENT_SECRET || "some-client-secret",
}),
});
const { access_token: accessToken } = await tokenResponse.json();
// 2. Send the access token as a Bearer token with every API request
const response = await fetch("https://api.tideform.io/graphql", {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `query spotPrice {
latestSpotPrices(input: { portIds: ["sgsin"], fuelGradeIds: ["mgo"] }) {
port { id name }
fuelGrade { id description }
latestPrices { publishedDate price band68Lower band68Upper }
}
}`,
}),
});
const { data } = await response.json();
The code samples on the other pages start from step 2 and assume the access token is stored in access_token (Python) or accessToken (JavaScript).