Connecting to AI Core
The SAP Cloud SDK for AI uses the Destination concept of the SAP Cloud SDK to connect to AI Core. This allows for a seamless integration in SAP BTP and offers a wide variety of managing the credentials for the connection.
Using a Service Binding
By default, the SAP Cloud SDK for AI will look for an aicore
service binding in the environment of the application.
When running on SAP BTP, create an instance of the AI Core service and bind it to your application.
- In Cloud Foundry, it's accessed from the
VCAP_SERVICES
environment variable. - In Kubernetes / Kyma environments, you have to mount the service binding as a secret instead, for more information refer to this documentation.
If no service binding is found and no destination is explicitly provided, the SDK throws an exception:
Could not find service credentials for AI Core. Please check the service binding.
To run the code outside of SAP BTP (e.g., locally), provide the necessary credentials manually.
Providing a Service Binding Locally
For local development, you can provide the service credentials locally in several ways.
Using CAP Hybrid Testing
When developing a CAP application, you can use the hybrid testing approach. The following command will bind the credential of your AI Core service instance:
cds bind -2 AICORE_INSTANCE_NAME
Then run the application with the hybrid profile:
cds-tsx watch --profile hybrid
Using the AICORE_SERVICE_KEY
Environment Variable
You can provide the service credential in the AICORE_SERVICE_KEY
environment variable.
SAP Cloud SDK for AI will read it through process.env.AICORE_SERVICE_KEY
variable.
Create a service key for your AI Core service instance and copy the resulting JSON object.
Next, set the environment variable AICORE_SERVICE_KEY
, e.g., by creating a .env
file with the following content:
AICORE_SERVICE_KEY='{
"clientid": "...",
...
}'
Load the .env
file by using dotenv or running node --env-file=.env ...
.
For NextJS, see this documentation.
Using a Destination
Using the SAP BTP Destination Service
You can define a destination in the SAP BTP Destination Service and use that to connect to SAP AI Core.
How to create a Destination in the SAP BTP Cockpit
-
Create a new Destination in the SAP BTP Cockpit with the following properties:
- Name:
my-aicore
- Type:
HTTP
- URL:
[serviceurls.AI_API_URL]
- Proxy Type:
Internet
- Authentication:
OAuth2ClientCredentials
- Client ID:
[clientid]
- Client Secret:
[clientsecret]
- Token Service URL Type:
Dedicated
- Token Service URL:
[url]/oauth/token
Fill in the values for URL, client ID, client secret, and token service URL from the service key JSON. Make sure to add
/oauth/token
in the token service URL. - Name:
If you are using CAP, you can again use Hybrid Testing to bind the destination service to your application when running locally.
Ensure you have created an instance of the SAP BTP Destination Service and bound it to your application.
By default, the fetched destination is cached. Check the Cache Expiration section of the SAP Cloud SDK documentation for more details.
This destination can be used later when initializing a client or calling the execute()
method.
Creating a Custom Destination
You can register a destination for AI Core programmatically. For more details, refer to the Register Destination section of the SAP Cloud SDK documentation.