π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
Direct -> c4ai.conf.json
-> Env Variables
Direct Credentials
This approach has the highest priority, and other credentials will be overwritten by the provided ones.
Example:
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
Another way to pass credentials and configurations is via the c4ai.conf.json
file.
Note: Read more in the C4AI Config File section.
In case credentials have not been passed directly, the SDK will use credentials from the file in the project root folder.
Example:
# Create the c4ai.conf.json file
touch c4ai.conf.json
// Edit c4ai.conf.json and set credentials
{
"client_id": "YOUR_CLIENT_ID", // mandatory
"client_secret": "YOUR_CLIENT_SECRET" // mandatory
}
Then authentication will be done automatically during the request execution.
Environment Variables
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:
# Create the .env file
touch .env
# 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:
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();
Note: Do not forget to include the
.env
file in.gitignore
to prevent credentials from being pushed to the repository.
Last updated
Was this helpful?