In this lesson, you will see what a Query API looks like and how to use APIs to manage both customer and customer activity data within the SAP Customer Data Platform.
Objective
In this lesson, you will see what a Query API looks like and how to use APIs to manage both customer and customer activity data within the SAP Customer Data Platform.
We can leverage the Query APIs to search, retrieve, and delete customer and customer activity data within the SAP Customer Data Platform.
Use the Query APIs to:
Profile Data | Activity Data |
---|---|
Retrieve customer profile data | Retrieve customer activities data |
Delete customer profile data | Retrieve a customer activities overview |
The following are some of the Query API endpoints:
Query API Endpoints | |
---|---|
Query All Customers | GET /api/businessunits/{businessUnitId}/views/{viewId}/customers |
Query Single Customer | GET /api/businessunits/{businessUnitId}/views/{viewId}/customers/{customerId} |
Delete Single Customer | DELETE /api/businessunits/{businessUnitId}/views/{viewId}/customers/{customerId} |
Query Activities from a Single Customer | GET /api/businessunits/{businessUnitId}/views/{viewId}/customers/{customerId}/activities |
Here is an example of a Query API call:
The above image is an example of a Query API call displaying the query and authentication parameters, along with the JSON response payload. The endpoint URL includes query parameters for query, userKey, secret, and purposesIds.
The labels indicate:
The response body is shown in JSON format, containing customer activity data with fields like docSize, _id, created, updated, and attributes.
The API call returns all the activities (Order in this case) for the specified customer, in the specified customer profile view (Unified Profile in this case), and under the specified Processing Purpose. The RESTful API response is in the form of a JSON object and a successful HTTP status of 200 response will include the above attributes.
To query customer profiles instead of activities, the query string would as shown below, but with the addition of a WHERE clause to narrow down the customer profiles returned.
query = select * from profile
The SAP Customer Data Platform customer data queries use SQL syntax, although some standard SQL keywords are not supported. For example, the (GROUP BY) SQL clause is unsupported, and using it in the query string will produce an error.
You must write the query string clauses in the following order to avoid generating an error:
Here are some examples of query usage:
1SELECT * FROM profile WHERE having(segments, (segments.name = "segment name" and segments.value = "segment value"))
In this section we will identify common query API parameters used to build REST API calls to SAP Customer Data Platform.
1.1 The very first one is the BASE_URL, which prefixes the Endpoint URL for our REST API calls. For SAP Customer Data Platform this value is https://cdp.eu5.gigya.com/api.
1.2 Next, let’s find the value of the first Query API call parameters, the VIEW_ID . It determines whether you are querying for Unified Profiles or Contextual Customer Profiles. On the SAP Customer Data Platform Console main menu, choose Search under the Customers menu group.
By default, new Business Units will only hold Unified Customer Profile Views.
Note
If you have both Unified and Contextual Customer Profile Views, the system will present you with a dropdown to choose between Unified and Contextual Customer Profile views. Business units created before 15 April 2024 had both views available by default. To help manage costs, business units created after that date only have the Unified Contextual Profile view enabled. If you choose to configure a Contextual Customer Profile view for a new business unit, then you’ll see the dropdown.1.3 Copy the value of the business-unit path, which we’ll refer to it as BU_ID. In the screenshot below this value appears as 4_iNfbhGDrBGciUrxckeBfJA.
1.4 Copy the value of the viewId parameter, which we’ll refer to it as VIEW_ID. In the screenshot below this value is shown as HAPxPF10AHr1bASCaGU_dQ.
1.5 Back to the SAP Customer Data Platform Console’s main menu, choose Processing Purposes. Then click on the three dots of the Processing Purpose card and choose Edit.
1.6 Copy the last path part of the URL, which we’ll refer to as the PURPOSE_ID. In the screenshot below this value is shown as HFZPPZxxFQUebd3ksLoTaQ.
We now have all the common requirements for using the Query APIs: the Base URL, USER_KEY, SECRET_KEY, BU_ID, VIEW_ID, and PURPOSE_ID.
Note: Please refer to Unit 1 Lesson 1 Creating Admin Credentials for API Access in SAP Customer Data Platform on how to supply the values for USER_KEY and SECRET_KEY.
The last two specific Query API Parameters are CUSTOMER_ID (only present when retrieving data from a single customer), and QUERY parameter (when the API call allows custom search).
Result: You have successfully located the Query API Parameters.
2.1 Let’s now build a SAP Customer Data Platform REST API call that queries all customer profiles. The Endpoint URL template is as follows:
1BASE_URL/businessunits/BU_ID/views/VIEW_ID/customers?purposeIds=PURPOSE_ID&query=PROFILES_QUERY&userKey=USER_KEY&secret=SECRET_KEY
2.2 The PROFILES_QUERY placeholder refers to a SQL query that returns all customer profiles:
SELECT * FROM profile.
2.3 Replacing all the placeholders in all caps from the endpoint URL template, we get:
1https://cdp.eu5.gigya.com/api/businessunits/4_iNfbhGDrBGciUrxckeBfJA/views/HAPxPF10AHr1bASCaGU_dQ/customers?purposeIds=HFZPPZxxFQUebd3ksLoTaQ&query=SELECT%20%2A%20FROM%20profile&userKey=AI32XTaizCgb&secret=tnhJ6U6NPAiGbAcX2nwu%2F7V%2F3VXp5Cmu
Note
Remember that the HTTP standard requires that you URL-encode each parameter value before inserting them into the URL above.2.4 After copying this URL to a browser, or using your preferred REST-compliant client application, you can see the JSON payload response containing a list of customer profiles such as the one shown in the image below. Let’s use the opportunity to copy the value of profiles[0]._id and refer to it later in this lesson as CUSTOMER_ID.
3.1 To search for a single customer profile, use the following endpoint URL template:
1BASE_URL/businessunits/BU_ID/views/VIEW_ID/customers/CUSTOMER_ID?purposeIds=PURPOSE_ID&userKey=USER_KEY&secret=SECRET_KEY
3.2 After replacing the placeholders in all caps, we get:
1https://cdp.eu5.gigya.com/api/businessunits/4_iNfbhGDrBGciUrxckeBfJA/views/HAPxPF10AHr1bASCaGU_dQ/customers/pQOoi2J5hFi5k3d3aEbVtu16kFSziPSv?purposeIds=HFZPPZxxFQUebd3ksLoTaQ&userKey=AI32XTaizCgb&secret=tnhJ6U6NPAiGbAcX2nwu%2F7V%2F3VXp5Cmu
3.3 After opening the above URL in a browser, we are presented with the following JSON payload response, containing the customer profile of a single customer:
4.1 To search for a single customer activity, use the following endpoint URL template:
1BASE_URL/businessunits/BU_ID/views/VIEW_ID/customers/CUSTOMER_ID/activities?purposeIds=PURPOSE_ID&query=ACTIVITIES_QUERY&userKey=USER_KEY&secret=SECRET_KEY
4.2 This API call is designed to provide activity records for a single customer and activity type. It requires a query referencing a single activity name that returns customer activity records in the JSON payload response. For example, to see all orders from a given customer, we would use the following query parameter value:
SELECT * FROM Orders
4.3 After replacing the placeholders in all caps, we get:
1https://cdp.eu5.gigya.com/api/businessunits/4_iNfbhGDrBGciUrxckeBfJA/views/HAPxPF10AHr1bASCaGU_dQ/customers/pQOoi2J5hFi5k3d3aEbVtu16kFSziPSv/activities?purposeIds=HFZPPZxxFQUebd3ksLoTaQ&query=SELECT%20%2A%20FROM%20OrdersuserKey=AI32XTaizCgb&secret=tnhJ6U6NPAiGbAcX2nwu%2F7V%2F3VXp5Cmu
4.4 After opening the above URL in a browser, we are presented with the following JSON payload response containing a single customer profile:
The following video demonstrates the full sequence of steps given above:
In this video we demonstrated the full sequence of steps as detailed in the lesson above. We first located the common query API parameters and built and executed the three query API calls shown in this lesson: search all customer profiles, a single customer profile, and activity records of a single customer.
In this lesson, you learned the SAP Customer Data Platform query API and its SQL like syntax specification, how to construct a query API call by specifying the query and authentication parameters to retrieve customer profile and activity data within the SAP Customer Data Platform.
Log in to track your progress & complete quizzes