Edit in GitHubLog an issue

Use CJA API with Python

Python is a powerful and easy to learn programming language. It has several libraries that allow you to create API calls. This page uses the requests library to make API calls to Customer Journey Analytics.

Prerequisites#

Make sure that you meet all requirements on the Getting started page, including:

  • Your user account belongs to the correct product profile in the Adobe Admin Console
  • An API client created in the Adobe I/O Console
  • You have generated a JWT (JSON Web Token), via the Service Account Integration or by your own script.

1. Preparing your header#

Once you possess your token, you can prepare a header to realize the requests with the requests module. In requests, the header is a dictionary, consisting of key value pairs. The CJA API requires 3 components on your header:

  • Your Token with the authorization key and "Bearer " as a start.
  • Your Organization ID with the x-gw-ims-org-id key
  • Your client ID with the x-api-key key.

For example, your dictionary would look similar to the following example:

Copied to your clipboard
1myHeader = {
2 "x-gw-ims-org-id" : "EXAMPLE@AdobeOrg",
3 "x-api-key" : "example-client-id",
4 "authorization" : "Bearer example-authorization-value"
5}

2. Sending the request#

Once you have your header ready, you can start loading and using the requests module. For example you can send a request to the /aresconfig/users/me endpoint:

Copied to your clipboard
1import requests
2res = requests.get("https://cja.adobe.io/aresconfig/users/me", headers=myHeader)

3. Using responses from the CJA API#

Once you receive a response from Adobe, you can use the .json() method on it to return the data returned by the CJA API. In the above example, the response is captured in the res variable. The below example uses this res variable to parse the response into a new variable.

Copied to your clipboard
myData = res.json()

The myData variable would contain a value similar to the following:

Copied to your clipboard
1{
2 'id': 'EXAMPLE@techacct.adobe.com',
3 'email': 'example@techacct.adobe.com',
4 'fullName': ''
5}

Other use-cases#

Sending data with query parameters#

Query parameters are elements following the question mark in a URL. For example, ?param=value.

When requesting the data views for a company, you use the following path: /datagroups/dataviews. However, a normal request (requests.get("https://cja.adobe.io/datagroups/dataviews",headers=myHeader)) only returns the following response:

Copied to your clipboard
1{
2 'content': [
3 {'id': 'dv_exampleid1'},
4 {'id': 'dv_exampleid2'},
5 {'id': 'dv_exampleid3'}
6 ],
7 'totalPages': 1,
8 'totalElements': 3,
9 'numberOfElements': 3,
10 'number': 0,
11 'firstPage': True,
12 'lastPage': True,
13 'sort': None,
14 'size': 10
15}

This is not typically useful as you miss some elements that easily identify the data view. If you use the name and owner query parameters, you can retrieve more interesting information. For example:

Copied to your clipboard
1parameters = {'expansion':'name,owner'}
2requests.get("https://cja.adobe.io/datagroups/dataviews", headers=myHeader, params=parameters)

Now the response contains additional information:

Copied to your clipboard
1{
2 'content': [{
3 'name': 'Web Call Example Data View',
4 'owner': {'imsUserId': 'EXAMPLE1@AdobeID', 'name': None},
5 'id': 'dv_exampleid1'
6 }, {
7 'name': 'All Datasets Example',
8 'owner': {'imsUserId': 'EXAMPLE2@AdobeID', 'name': None},
9 'id': 'dv_exampleid2'
10 }, {
11 'name': 'Example (default)',
12 'owner': {'imsUserId': 'EXAMPLE3@AdobeID', 'name': None},
13 'id': 'dv_exampleid3'
14 }
15 ],
16 'totalElements': 3,
17 'totalPages': 1,
18 'numberOfElements': 3,
19 'number': 0,
20 'firstPage': True,
21 'lastPage': True,
22 'sort': None,
23 'size': 10
24}

Sending data in the body of the request#

You can update or create a component by sending data in the body of a request. That type of API call is typically either a POST or PUT request.

Data sent to the Customer Journey Analytics API is typically through a python dictionary. The easiest method to send data is to stringify the JSON element via the json module. You can use the following example to help format the body of your API call.

Copied to your clipboard
1creationDataView = {
2 "name": "string",
3 "description": "string",
4 "owner": {
5 "imsUserId": "string",
6 "name": "string"
7 },
8 "isDeleted": true,
9 "parentDataGroupId": "string",
10 "segmentList": [
11 "string"
12 ],
13 "currentTimezoneOffset": 0,
14 "timezoneDesignator": "string",
15 "modifiedDate": "YYYY-08-24T14:15:22Z",
16 "createdDate": "YYYY-08-24T14:15:22Z",
17 "organization": "string",
18 "modifiedBy": "string",
19 "curationEnabled": true,
20 "recentRecordedAccess": "YYYY-08-24",
21 "sessionDefinition": [{}],
22 "curatedComponents": [{}],
23 "externalData": {
24 "externalId": "string",
25 "externalParentId": "string"
26 },
27 "containerNames": {
28 "event": "string",
29 "session": "string",
30 "people": "string"
31 },
32 "id": "string"
33}

Once you have this definition established (creationDataView in the above example), you can use it in an API call. The following example uses the Data view creation API and includes the above JSON object in its body:

Copied to your clipboard
1import json
2resCreation = requests.post("https://cja.adobe.io/datagroups/dataviews", headers=myHeader, data=json.dumps(creationDataView))

The response from Adobe would looks similar to the following:

Copied to your clipboard
1resCreation.json()
2
3## Response from Adobe
4{
5 "name": "string",
6 "description": "string",
7 "owner": {
8 "imsUserId": "string",
9 "name": "string"
10 },
11 "isDeleted": true,
12 "parentDataGroupId": "string",
13 "segmentList": [
14 "string"
15 ],
16 "currentTimezoneOffset": 0,
17 "timezoneDesignator": "string",
18 "modifiedDate": "YYYY-08-24T14:15:22Z",
19 "createdDate": "YYYY-08-24T14:15:22Z",
20 "organization": "string",
21 "modifiedBy": "string",
22 "curationEnabled": true,
23 "recentRecordedAccess": "YYYY-08-24",
24 "sessionDefinition": [{}],
25 "curatedComponents": [{}],
26 "externalData": {
27 "externalId": "string",
28 "externalParentId": "string"
29 },
30 "containerNames": {
31 "event": "string",
32 "session": "string",
33 "people": "string"
34 },
35 "id": "string"
36}
Copyright © 2021 Adobe. All rights reserved.