Authentication
Short answer
Every Sirv REST API request needs a bearer token. Send your API client's ID and secret to POST /v2/token and you get a token that lasts 20 minutes by default. Add it to each request as an authorization header: the word Bearer, then the token. When it expires, ask for a new one.
After reading this you will know
- How to get a bearer token from your client ID and secret
- How long a token lasts and how to change it
- Sample code in 9 languages
This is the only command you call without a token. Send the token it returns with every other request.
Get bearer token
Section titled “Get bearer token”https://api.sirv.com/v2/token
Every API call will be authenticated with a bearer token, so your first step is to obtain a bearer token.
Bearer tokens expire after 20 minutes (1200 seconds). If your token expires, request a new one, then continue with your work.
After each API request you submit, you’ll receive a response that contains useful information, including an expiresIn field (in seconds) telling you how long remains before your bearer token expires.
Request body (JSON)
| Field | Type | Required | Example | Description |
|---|---|---|---|---|
clientId | string | Yes | CLIENT_ID | API client ID |
clientSecret | string | Yes | CLIENT_SECRET | API client secret |
expiresIn | number | No | 1200 | Expiration TTL for token in seconds. Up to 604800. |
Example request
// Your API client ID and secret, in the SIRV_CLIENT_ID and SIRV_CLIENT_SECRET environment variablesconst res = await fetch('https://api.sirv.com/v2/token', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ "clientId": process.env.SIRV_CLIENT_ID, "clientSecret": process.env.SIRV_CLIENT_SECRET }),});console.log(res.status, await res.text());import requests
res = requests.post( 'https://api.sirv.com/v2/token', headers={'content-type': 'application/json'}, json={ "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET" },)print(res.status_code, res.text)curl --request POST \ --url 'https://api.sirv.com/v2/token' \ --header 'content-type: application/json' \ --data '{"clientId":"CLIENT_ID","clientSecret":"CLIENT_SECRET"}'import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;
HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.sirv.com/v2/token")) .header("content-type", "application/json") .method("POST", HttpRequest.BodyPublishers.ofString(""" { "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET" } """)) .build();HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());System.out.println(response.statusCode() + " " + response.body());using System.Net.Http.Headers;
using var client = new HttpClient();var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.sirv.com/v2/token");request.Content = new StringContent("""{ "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET"}""", System.Text.Encoding.UTF8, "application/json");var response = await client.SendAsync(request);Console.WriteLine($"{(int)response.StatusCode} {await response.Content.ReadAsStringAsync()}");<?php$ch = curl_init('https://api.sirv.com/v2/token');curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => ['content-type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'clientId' => 'CLIENT_ID', 'clientSecret' => 'CLIENT_SECRET' ]), CURLOPT_RETURNTRANSFER => true,]);echo curl_exec($ch);package main
import ( "fmt" "io" "net/http" "strings")
func main() { body := strings.NewReader(`{ "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET" }`) req, _ := http.NewRequest("POST", "https://api.sirv.com/v2/token", body) req.Header.Set("content-type", "application/json") res, err := http.DefaultClient.Do(req) if err != nil { panic(err) } defer res.Body.Close() out, _ := io.ReadAll(res.Body) fmt.Println(res.Status, string(out))}require 'net/http'
uri = URI('https://api.sirv.com/v2/token')req = Net::HTTP::Post.new(uri)req['content-type'] = 'application/json'req.body = <<~'JSON' { "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET" }JSONres = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }puts res.code, res.bodyimport Foundation
var request = URLRequest(url: URL(string: "https://api.sirv.com/v2/token")!)request.httpMethod = "POST"request.setValue("application/json", forHTTPHeaderField: "content-type")request.httpBody = #"""{ "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET"}"""#.data(using: .utf8)let (data, response) = try await URLSession.shared.data(for: request)print((response as! HTTPURLResponse).statusCode, String(decoding: data, as: UTF8.self))Example response
{ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "expiresIn": 1200, "scope": [ "account:read", "account:write" ]}