# How to Authenticate to the API

NVIDIA Run:ai APIs are accessed using **bearer tokens**. A token can be obtained using an access key, which serves as the authentication method for both users and service accounts.

An access key consists of a client ID and client secret, which are used to generate an access token for API authentication. Once a token is obtained, it can be included in the `Authorization` header of subsequent API requests.

* To create service accounts for your organization, see [Service accounts](/saas/infrastructure-setup/authentication/service-accounts.md)
* To create your own access keys, see [User access keys](/saas/settings/user-settings/user-access-keys.md)

## Assign Access Rules

After you have created a new service account, you will need to assign it access rules. To assign access rules to the service account, see [Access rules](/saas/infrastructure-setup/authentication/accessrules.md). Make sure you assign the correct rules to your service account. Use the [Roles](/saas/infrastructure-setup/authentication/roles.md) to assign the correct access rules.

## Request an API Token

Use the client credentials created to get a temporary token to access NVIDIA Run:ai as follows.

### Example Command to Get an API Token

Replace `<COMPANY-URL>` below with:

* For SaaS installations, use `<tenant-name>.run.ai`
* For self-hosted use the NVIDIA Run:ai user interface URL.

{% tabs %}
{% tab title="cURL" %}

```bash
curl -X POST \ 
  'https://<COMPANY-URL>/api/v1/token' \ 
  --header 'Accept: */*' \ 
  --header 'Content-Type: application/json' \ 
  --data-raw '{ 
  "grantType":"client_credentials", 
  "clientId":"<CLIENT ID>", 
  "clientSecret" : "<CLIENT SECRET>" 
}'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests 
import json 
reqUrl = "https://<COMPANY-URL>/api/v1/token" 
headersList = { 
 "Accept": "*/*", 
 "Content-Type": "application/json" 
} 
payload = json.dumps({ 
  "grantType":"client_credentials", 
  "clientId":"<CLIENT ID>", 
  "clientSecret" : "<CLIENT SECRET>" 
}) 
response = requests.request("POST", reqUrl, data=payload, headers=headersList) 
print(response.text)
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    url := "https://<COMPANY-URL>/api/v1/token"
    payload := map[string]string{
        "grantType":    "client_credentials",
        "clientId":     "<CLIENT ID>",
        "clientSecret": "<CLIENT SECRET>",
    }
    jsonPayload, err := json.Marshal(payload)
    if err != nil {
        panic(err)
    }

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
    if err != nil {
        panic(err)
    }
    req.Header.Set("Accept", "*/*")
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
}
```

{% endtab %}
{% endtabs %}

### Response

The API response will look as follows:

```json
{
  "accessToken": "<TOKEN>", 
}
```

To call NVIDIA Run:ai REST APIs, the application must pass the retrieved `accessToken` as a *Bearer token* in the Authorization header of your HTTP request.

## Additional Code Examples

For more comprehensive code examples demonstrating how to authenticate and interact with the NVIDIA Run:ai API, visit the official [NVIDIA Run:ai API Examples repository](https://github.com/run-ai/docs/tree/master/examples/api) on GitHub.

This repository contains ready-to-use code samples in multiple programming languages, including:

* [Python examples](https://github.com/run-ai/docs/tree/master/examples/api/python_client)
* [Go examples](https://github.com/run-ai/docs/tree/master/examples/api/go_client)

These examples cover common authentication flows, API requests, and best practices for securely accessing the NVIDIA Run:ai platform. You can use them as a reference or starting point for your own integrations.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://run-ai-docs.nvidia.com/api/getting-started/how-to-authenticate-to-the-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
