# Authentication

This is the only command you call without a token. Send the token it returns with every other request.

## Get bearer token

**POST**`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

-   [Node.js](#tab-panel-0-0)
-   [Python](#tab-panel-0-1)
-   [cURL](#tab-panel-0-2)
-   [Java](#tab-panel-0-3)
-   [C#](#tab-panel-0-4)
-   [PHP](#tab-panel-0-5)
-   [Go](#tab-panel-0-6)
-   [Ruby](#tab-panel-0-7)
-   [Swift](#tab-panel-0-8)

```js
// Your API client ID and secret, in the SIRV_CLIENT_ID and SIRV_CLIENT_SECRET environment variables
const 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());
```

```python
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)
```

```bash
curl --request POST \
  --url 'https://api.sirv.com/v2/token' \
  --header 'content-type: application/json' \
  --data '{"clientId":"CLIENT_ID","clientSecret":"CLIENT_SECRET"}'
```

```java
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());
```

```csharp
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
<?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);
```

```go
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))
}
```

```ruby
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"
  }
JSON
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
puts res.code, res.body
```

```swift
import 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

```json
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "expiresIn": 1200,
  "scope": [
    "account:read",
    "account:write"
  ]
}
```
