Skip to main content

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.

Prerequisite for using the SAP Cloud SDK for AI is to have an instance of the AI Core service created.

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. The SAP Cloud SDK for AI uses @sap/xsenv to read the service binding.

When running on SAP BTP (Cloud Foundry, Kyma, Gardener), bind the AI Core service instance to your application.

  • In Cloud Foundry, binding the service will result in the AI Core credentials being included in the VCAP_SERVICES environment variable.
  • In Kubernetes / Kyma environments, you have to use the SAP BTP Service Operator to create the binding. That will create a Kubernetes secret, which must be mounted to the application pod. By default, the expected mount path is /etc/secrets/sapcp/aicore/<instance-name> For more information refer to the documentation from @sap/xsenv.

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.
tip

Since SAP Cloud SDK for AI uses @sap/xsenv for reading the service binding, refer to @sap/xsenv for more information about service lookup, VCAP_SERVICES environment variable in Cloud Foundry, secret mounting path in Kyma / Kubernetes and many more.

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
  1. Create a service key for your AI Core service instance.

  2. 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.

tip

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.