Applying the REST API

Objective

After completing this lesson, you will be able to discover how to use REST APIs to consume SAP Customer Data Cloud services and implement various identity flows.

SAP Customer Data Cloud REST APIs

The SAP Customer Data Cloud REST API is a powerful interface for managing user identities, social login flows, and preference data.

The core of the SAP Customer Data Cloud service is a REST-like interface. This means that API method calls are made over the internet by sending HTTP GET, POST, or DELETE requests to the SAP Customer Data Cloud REST API server, and the response is returned as JSON.

SAP Customer Data Cloud global Data Centers regional distribution diagram with API endpoint structure and REST API documentation navigation menu.

For more information, refer to REST API documentation in the SAP Help Portal.

To find the data center that your site is associated with, refer to the Finding Your Data Center documentation in the SAP Help Portal.

Purpose and Value of REST APIs in SAP Customer Data Cloud

REST APIs are essential for interacting with SAP Customer Data Cloud programmatically. They provide a standardized way to access and manage user identities, data, and configurations. Here's why REST APIs are valuable:

  • Integration: REST APIs enables seamless integration of SAP Customer Data Cloud with various applications, systems, and platforms, allowing you to extend the functionality of SAP Customer Data Cloud beyond its native interface.

  • Automation: You can automate identity management tasks such as user registration, login, profile updates, and data synchronization, reduce manual effort and improve efficiency.

  • Customization: REST APIs allows you to customize the behavior of SAP Customer Data Cloud to meet specific business requirements, such as implementing custom authentication flows or integrating with third-party services.

  • Flexibility: REST APIs offer flexibility in how you interact with SAP Customer Data Cloud, allowing you to use different programming languages, tools, and frameworks to build custom solutions.

Authentication and Authorization Mechanisms for SAP Customer Data Cloud REST APIs

To access the SAP Customer Data Cloud REST APIs, you need to authenticate and authorize your requests. SAP Customer Data Cloud offers a Rest-like Web API that can be consumed via HTTPS requests and requires one of the following authentication methods:

  • An HTTP bearer token header.

  • A combination of App Key and App Secret.

  • A combination of User Key and User Secret.

  • A Partner Secret.

The four authentication methods of calling the SAP Customer Data Cloud REST APIs, with the bearer token the best and most recommended method, and the partner secret key the worst and least discouraged method.

Note

Most APIs also require HTTP Request Payload or Query parameters.

Requests to the REST API should be signed for security purposes. The recommended best practice is to generate a bearer token for the signature.

The above list is ordered accordingly with best practices, with item 4 being the worst way (you must avoid it) and the first one the best.

For more information, refer to the REST APIs with the SAP Customer Data Cloud Authorization Method documentation in the SAP Help Portal.

Here’s an example curl command to call the accounts.getSchema API.

Code Snippet
1234567
curl -X POST \ https://accounts.us1.gigya.com/accounts.getSchema \ -H 'content-type: application/x-www-form-urlencoded' \ --data-urlencode "apiKey=[Enter-Your-API-Key-Here]" \ --data-urlencode "secret=[Enter-Your-Secret-Key]" \ --data-urlencode "scope=site" \ --data-urlencode "filter=explicitOnly" \

This is one of the recommended approaches to use the SAP Customer Data Cloud API. It allows administrators to provide appropriate access and can be audited, extended, or limited based on needs.

App Keys are essentially the same as User Keys, except they are intended to be used by applications, such as mobile apps or server-side apps.

You can create as many User Keys as needed by inviting users or creating App Keys (the better way) in the Administration section of the SAP Customer Data Cloud Console.

Application Key has higher rate limits than standard user keys, but their actions aren't reviewed.

Constructing and Executing REST API Requests

To interact with the SAP Customer Data Cloud REST APIs, you need to construct and execute HTTP requests with the appropriate headers, parameters, and payload. Here's a breakdown of the process:

  • Identify the API Endpoint: Determine the correct API endpoint for the operation you want to perform (e.g., accounts.register, accounts.getAccountInfo).

  • Choose the HTTP Method: Select the appropriate HTTP method (GET, POST, DELETE) based on the API documentation.

  • Set the Headers: Include required headers such as Content-Type (usually application/x-www-form-urlencoded or application/json) and Authorization(if using bearer tokens).

  • Provide Parameters: Include authentication parameters like userKey and secret(if not using bearer tokens) and other required parameters as query parameters or in the request body (payload).

  • Sign the Request (if required): Generate a signature or bearer token based on your authentication method and include it in the request.

  • Execute the Request: Use a tool like curl, Postman, or an HTTP client library in your programming language to send the request to the SAP Customer Data Cloud API server.

Creating Permission Groups, Applications, Custom Data Fields and Registering a User Account with REST APIs - SAP Customer Data Cloud

In this video, we will walk you through creating a permission group and application in the SAP Customer Data Cloud Console. You will learn how to use the REST APIs to add a custom data field to the account schema and register a user.

Using Bearer Token HTTP Header

When creating a new application in the Console, an asymmetric RSA key-pair is assigned. Copy the private key and store it securely.

SAP Customer Data Cloud Application administration interface demonstrating application setup with user credentials, private key, and permission group assignment configuration.

You can sign an API request to SAP Customer Data Cloud using an HTTP bearer token. This replaces the application/user key and secret signature method. To do so, you should create a JWT, then hash the JWT using the RSA key to create a bearer token and sign the API request using that token.

For example, the token below is a JWT token itself.

HTML
1
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ilt1c2Vya2V5XSJ9.eyJpYXQiOjE1NDgxOsdkj

For more information, refer to the Signing Requests to SAP Customer Data Cloud documentation in the SAP Help Portal.

Signing API Request using Bearer Token - SAP Customer Data Cloud

In this video, we will show you how to authenticate your API requests to SAP Customer Data Cloud using a bearer token.

Setting and Getting Account Data in the Account Store

  • accounts.getAccountInfo - This endpoint gets the account data of a single account by its UID. It’s typically called after a successful login to the platform.

  • accounts.setAccountInfo -This endpoint sets account data into a user's account. The endpoint accepts a list of optional parameters each defining a field/object in the account. The parameters that are passed in the request modify the relevant fields, and the other fields remain unchanged.

  • accounts.search - This endpoint searches and retrieves data from SAP Customer Data Cloud's Accounts Storage using an SQL-like query. SQL queries are converted into SAP Customer Data Cloud's proprietary query language.

SAP Customer Data Cloud Accounts APIs three core operations diagram: getAccountInfo for retrieval, setAccountInfo for data storage, and search for query operations on user account database.

Core Accounts Registration Flow

The following API calls are used in succession to register a new user account.

  1. accounts.initRegistration - This begins the registration process and provides a regToken, used for the next API call. A regToken  (registration token), generated by accounts.initRegistration, is not required when using this endpoint at server-side, with a signed request.

  2. accounts.register -  This endpoint uses the regToken  from step 1 and adds the e-mail and password to the account . Profile data can also be set and the account registration finalized, but if further details are required then a call to accounts.setAccountInfo  is required (next step).

  3. accounts.setAccountInfo -  If the user enters more details, this endpoint updates the partially-created account with the regToken generated in step 2.

  4. accounts.finalizeRegistration - This endpoint completes the account registration. The account is created and can be used to login or wait for account verification.

SAP Customer Data Cloud account registration API workflow showing four sequential steps: initRegistration, register, setAccountInfo, and finalizeRegistration.

For more information, refer to the Accounts REST documentation in the SAP Help Portal.

Reading and Changing Schema for Account Store

These endpoints are used to retrieve and customize your schema in the SAP Customer Data Cloud Accounts Storage.

  • accounts.getSchema  - This endpoint retrieves the schema of the Profile, Subscriptions, Internal, Addresses objects and the Custom Data object in SAP Customer Data Cloud's Accounts Storage.

  • accounts.setSchema - This endpoint enables you to specify a schema for SAP Customer Data Cloud's Accounts Storage. Schemas apply to the Profile, Custom Data, Internal, Addresses, and Subscriptions object.

SAP Customer Data Cloud Accounts schema API operations diagram showing getSchema for retrieval and setSchema for configuration of account data structure.

For more information, refer to the accounts.getSchema REST and the accounts.setSchema REST documentation in the SAP Help Portal.

Reading and Changing Schema for (Object) Data Store (Server-side SDK)

These endpoints are used to retrieve and customize your schema in the SAP Customer Data Cloud Data Store.

  • ds.getSchema - This endpoint retrieves the schema of a specified data/object type in SAP Customer Data Cloud's Data Store (DS).

  • ds.setSchema - This endpoint allows specifying a schema for a data/object type in SAP Customer Data Cloud's Data Store (DS). The schema sets field names, data types, formatting, and encryption as well as client-side access restrictions. 

SAP Customer Data Cloud DataStore schema API operations diagram showing ds.getSchema for retrieval and ds.setSchema for configuration of datastore structure.

For more information, refer to the ds.getSchema REST and the ds.setSchema REST documentation in the SAP Help Portal.

Setting and Getting Object Data in the Data Store

The Data Store is only available for use with fully registered accounts.

  • ds.store - Stores an object data in SAP Customer Data Cloud's Data Store (DS).

  • ds.get  - Retrieves an object or the specified data from SAP Customer Data Cloud's Data Store.

  • ds.search - Searches and retrieves data from SAP Customer Data Cloud's Data Store (DS) using an SQL-like query.  

SAP Customer Data Cloud DataStore schema API operations diagram showing ds.store for data storage, ds.get for retrieval, and ds.search for for query operations on user account database.

Note

For security reasons, this method is not available for client-side SDKs, only for server-side SDKs.

For more information, please refer to the Data Store REST documentation in the SAP Help Portal. 

Interpreting API Responses and Handling Errors

SAP Customer Data Cloud REST APIs return responses in JSON format. Understanding the structure of these responses and handling potential errors is crucial for building robust integrations.

  • Successful Responses: A successful API call typically returns a response with an of errorCode of 0 and a statusCode of 200. The response body contains the requested data or confirmation of the operation.

  • Error Responses: If an error occurs, the response will include a non-zero errorCode and a corresponding status code. The response body may also contain an errorMessage with details about the error.

Common error codes include:

  • 400002: Invalid parameter value

  • 400009: Missing required parameter

  • 200001: Invalid API key

  • 206001: Missing required fields

When handling errors, it's essential to log the error code, status code, and error message for debugging purposes. You should also implement appropriate error handling logic in your application to gracefully handle errors and provide informative feedback to the user.

Best Practices for Securing and Optimizing SAP Customer Data Cloud REST API Integrations

Securing and optimizing your SAP Customer Data Cloud REST API integrations is crucial for maintaining data integrity, ensuring performance, and preventing unauthorized access. Here are some best practices to consider:

  • Use Bearer Tokens for Authentication: As highlighted earlier, using bearer tokens (JWTs) is the recommended approach for authenticating API requests. This method is more secure than using API keys directly, as it allows for more granular control over access and can be easily revoked if necessary.

  • Implement Proper Input Validation: Always validate the data you send to the API. This helps prevent injection attacks and ensures that the data conforms to the expected format.

  • Rate Limiting and Throttling: Implement rate limiting to prevent abuse and ensure fair usage of the API. This can protect your system from denial-of-service (DoS) attacks and ensure that resources are available for legitimate users.

  • Monitor API Usage: Regularly monitor API usage to identify potential security threats or performance bottlenecks. Tools like SAP Integration Suite can help you track API calls, response times, and error rates.

  • Use HTTPS: Always use HTTPS to encrypt data in transit. This protects sensitive information from being intercepted by malicious actors.

  • Error Handling: Implement robust error handling to gracefully handle API errors and provide informative error messages to users.

Summary

  • REST APIs are the core of SAP Customer Data Cloud, enabling interaction through HTTP requests and JSON responses.

  • Authentication methods include bearer token, application key, user key and partner secret, with bearer tokens being the recommended security practice.

  • Key API endpoints like accounts.getAccountInfo, accounts.setAccountInfo, and accounts.search facilitate account data management.

  • The core account registration API flow involves a sequence of methods: accounts.initRegistration, accounts.register, accounts.setAccountInfo, and accounts.finalizeRegistration.

  • Schema management is achieved through accounts.getSchema and accounts.setSchema for the Account Store, and ds.getSchema and ds.setSchema for the Data Store.