Free JWT Trial Token
Get a free trial JWT refresh token
To try out the API you can request a Trial API Token from the form on the Get an API access page.
API trial includes a limited JWT token you can use to try, test and begin to work with Vainu API. This token is limited both in time and features, but it allows you to look into our data and possibilities Vainu API offers.
JWT authentication
JWT authentication works with long-lived refresh token and short-lived access tokens. The access token is used to call the API, and the refresh token generates new access tokens when needed. The lifetime for the refresh token is the default time for the trial (30 days). Each access token is valid for 12 hours, and a new one must be generated when the access token expires.
Using Postman
Make a separate POST request to the refresh endpoint with the refresh token to receive a new access token.
You can use the access token with Postman by selecting the Authorization tab. From the tab, select Bearer token type from the dropdown and insert your access token to the token field.
JWT authentication workflow:
- Use refresh token with a POST request to the following endpoint: https://api.vainu.io/api/v2/token_authentication/refresh/
import requests
# Insert Refresh Token you received here:
payload = {"refresh": REFRESH_TOKEN}
url = "https://api.vainu.io/api/v2/token_authentication/refresh/"
response = requests.post(url, payload)
# Now you have an Access Token that is valid for 12 hours:
access_token = response.json()["access"]
import http.client
import json
conn = http.client.HTTPSConnection("api.vainu.io")
payload = "{\"refresh\": TOKEN_GOES_HERE}"
headers = { 'Content-Type': "application/json" }
conn.request("POST", "/api/v2/token_authentication/refresh/", payload, headers)
res = conn.getresponse()
data = res.read()
print(json.loads(data.decode("utf-8")))
const axios = require('axios');
axios.post('https://api.vainu.io/api/v2/token_authentication/refresh/', {
// Insert Refresh toke you received here:
refresh: REFRESH_TOKEN
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
- Use the access token you received in step 1 to perform queries in the API. The access token is sent as a Bearer token in the authorization header.
import requests
headers = {'Authorization': f"Bearer {access_token}"}
url = (
"https://api.vainu.io/api/v2/companies/"
"?country=FI&company_name__startswith=vainu finland oy&limit=1&fields=company_name,business_id,domain"
)
response = requests.get(url, headers=headers)
print(response.json())
{'results': [{'domain': 'vainu.com', 'company_name': 'Vainu Finland Oy', 'business_id': '28229966'}]}
import http.client
import json
conn = http.client.HTTPSConnection("api.vainu.io")
payload = ""
token = AUTHORIZATION_TOKEN_GOES_HERE
headers = { 'Authorization': f"Bearer {token}" }
conn.request("GET", "/api/v2/companies/?country=FI", payload, headers)
res = conn.getresponse()
data = res.read()
print(json.loads(data.decode("utf-8")))
const axios = require('axios');
let url = (
"https://api.vainu.io/api/v2/companies/"
"?company_name__startswith=vainu finland oy&limit=1&fields=company_name,business_id,domain"
)
axios.get(url, {
headers: {'Authorization': "Bearer " + ACCESS_TOKEN }
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
-
Refresh the access token. Each access token is valid for 12 hours. After this, the token is invalid and it needs to be refreshed with the refresh token as in step 1.
-
If the refresh token expires, you need to contact Vainu to get a new refresh token.
Trial Token limitations
Trial tokens are limited both in expiration time and features. Following limits apply for the API Trial tokens:
Limitation | Trial API | Full API |
---|---|---|
Lifetime | 30 days | No limit |
Response | Maximum of 20 results per API request | Maximum of 1000 results per API request |
Endpoints | Only access to the https://api.vainu.io/api/v2/companies endpoint | Full access |
Concurrent requests | Only 1 concurrent request | 5 concurrent requests |
Amount of requests | Maximum 1000 requests per week | Up to unlimited |
Example on automatic token renewal
Following example(s) describe potential methods to implement automatic token renewal with refresh and access tokens on your projects.
/*
This example is using Axios library and it's interceptors to implement
automatic access token refreshing.
Interceptors expand the functionality of the Axios instance
and access/refresh tokens are handled automatically for each request created
with the instance.
We store our tokens to a global variable in the example.
In real life you should implement a store that saves those values.
*/
const axios = require('axios');
const vainuApiInstance = axios.create();
// Our dummy token storage
let tokenStore = {
refresh: "<INSERT YOUR REFRESH TOKEN HERE>",
access: ""
}
/*
Request interceptor for each API request.
Interceptor loads access token and sets it to a authorization header.
*/
vainuApiInstance.interceptors.request.use(
async config => {
// Load accessToken from your preferred storage
const accessToken = await getAccessToken()
// Set new headers based on loaded access token
config.headers = {
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json'
}
return config;
},
error => {
return Promise.reject(error)
});
/*
Response interceptor to listen for 401 errors. Interceptor tries to retry
once with new access token if the error received is 401 Unauthorized.
*/
vainuApiInstance.interceptors.response.use((response) => {
return response
}, async function (error) {
// Original request specs
const originalReq = error.config;
// Checking if the issue is the access token (401) and if we already refreshed it
if (error.response.status === 401 && !originalReq._refreshed) {
// Setting the refreshed status to the original request to prevent infinite requests
originalReq._refreshed = true;
// Refreshing the access token
const newAccessToken = await refreshAccessToken();
// Updating the headers with new access token
vainuApiInstance.defaults.headers.common['Authorization'] = 'Bearer ' + newAccessToken;
// Returning the original query for retry with new refreshed access token in place
return vainuApiInstance(originalReq);
}
return Promise.reject(error);
});
// Return access token from your storage.
async function getAccessToken() {
let ACCESS_TOKEN = tokenStore.access;
return ACCESS_TOKEN;
}
// Save access token to your storage.
async function saveAccessToken(newAccessToken) {
tokenStore.access = newAccessToken;
return tokenStore.access;
}
// Return refresh token from your storage
async function getRefreshToken() {
let REFRESH_TOKEN = tokenStore.refresh;
return REFRESH_TOKEN;
}
async function refreshAccessToken() {
// Refresh url of Vainu API
const url = "https://api.vainu.io/api/v2/token_authentication/refresh/";
// Load your refresh token from your storage
const refreshToken = await getRefreshToken();
// Refresh the access token
try {
const res = await axios.post(url, {refresh: refreshToken});
const newAccessToken = await saveAccessToken(res.data.access);
return newAccessToken;
} catch (error) {
return Promise.reject(error)
}
}
// Example request done with the instance
demoRequests();
async function demoRequests() {
const demoRequestUrl = "https://api.vainu.io/api/v2/companies/"
+ "?fields=company_name,business_id,domain"
+ "&country=FI&company_name__startswith=vainu&limit=1";
try {
const result = await vainuApiInstance.get(demoRequestUrl)
console.log(result.data.results);
} catch (error) {
console.log(error);
}
}
Updated about 1 year ago