> For the complete documentation index, see [llms.txt](https://docs.cloud4.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cloud4.ai/sdk/authentication.md).

# Authentication

## **Introduction**

C4AI SDK provides several ways to pass credentials and configurations:

* Directly through context
* Using the C4AI Config File
* Using Environment Variables

Each case may be useful for specific scenarios depending on your application architecture.

## **Priority**

In some cases, it's possible to combine several approaches to SDK configuration. For these scenarios, the priority of configurations should be taken into account. The standard priority is the same as above&#x20;

Direct -> `c4ai.conf.json` -> Env Variables

### **Direct Credentials**&#x20;

This approach has the highest priority, and other credentials will be overwritten by the provided ones.

*Example:*

```javascript
import { C4AI_SDK_Context } from "@c4ai-sdk/global/C4AISDKContext.class";

C4AI_SDK_Context.setCredentials(
    "C4AI--client::769e...YOUR_CREDS....04bc161",
    "C4AI--secret::5ca62...YOUR_CREDS....99249dab36bf4db"
);

/**
 * Not mandatory in real requests. 
 * Will authenticate automatically during request
 */
await C4AI_SDK_Context.authenticate();
```

### **Config File**&#x20;

Another way to pass credentials and configurations is via the `c4ai.conf.json` file.&#x20;

> &#x20;**Note:** *Read more in the* [Configurations & Env Variables](/sdk/configurations-and-env-variables.md#c4ai-config-file) *section.*&#x20;

In case credentials have not been passed directly, the SDK will use credentials from the file in the project root folder.

**Example:**

```bash
# Create the c4ai.conf.json file
touch c4ai.conf.json
```

```json
// Edit c4ai.conf.json and set credentials
{
    "client_id": "YOUR_CLIENT_ID", // mandatory
    "client_secret": "YOUR_CLIENT_SECRET" // mandatory
}
```

&#x20;Then authentication will be done automatically during the request execution.

### **Environment Variables**&#x20;

This is the most secure and recommended way to pass your credentials to the SDK. However, it will only be used if the configuration file `c4ai.conf.json` and direct credentials have not been provided.

**Example:**&#x20;

```bash
# Create the .env file
touch .env
```

```dotenv
# Add mandatory variables to .env
C4AI_CLIENT_ID=C4AI--client::769e...YOUR_CREDS....04bc161
C4AI_CLIENT_SECRET=C4AI--secret::5ca62...YOUR_CREDS....99249dab36bf4db
```

Then authenticate:

```javascript
import { config } from 'dotenv';
config(); // just for example
import { C4AI_SDK_Context } from "@c4ai-sdk/global/C4AISDKContext.class";

/**
 * Not mandatory in real requests. 
 * Will authenticate automatically during request
 */
await C4AI_SDK_Context.authenticate(); 
```

> &#x20;**Note:** *Do not forget to include the `.env` file in `.gitignore` to prevent credentials from being pushed to the repository.*
