Batches separate API actions. Each nested object in the data
array is an individual API action, and must include the required parameters for that method. You'll need to check each nested API method's documentation for info on required parameters and options.
To improve performance and reduce your billable API calls, you should batch multiple API requests that affect the same users as much as possible (see more on Reducing billable requests).
POST https://api.leanplum.com/api?action=multi
The
multi
call method is limited to 50 users and/or 500 actions in a single call. All calls in a batch with more than this will be ignored and the server will return a 403 status. Each unique user lookup in a request is a billable API call. See billing and costs for more.
param | description |
---|---|
appId | required The application ID. To find yours, select your app in the navigation column, and click Edit Apps. Under Keys, click Show. |
clientKey | required The Production key for your Leanplum App. |
apiVersion | required The version of the Leanplum API to use. The current version is 1.0.6. |
time | required The time at which the request was issued (UNIX time). This is used to resolve the time difference between Leanplum and the API caller when computing the times of events. |
data | required A JSON-encoded list of API methods to execute. All methods must be for the same app referred to by the appId parameter. Each data object must include the required arguments for its API action. |
Examples for batching with multi
multi
{
"appId": "YOUR_APP_ID",
"clientKey": "YOUR_PROD_KEY",
"apiVersion": "1.0.6",
"time": 1375295890,
"data": [
{
"action": "start",
"time": 1375295825,
"userAttributes": {
"Gender": "Male",
"Age": 25
},
"userId": "user1"
},
{
"action": "track",
"time": 1375295830,
"event": "Level Complete",
"params": {
"Level": 1,
"Score": 100
},
"userId": "user1"
}
]
}
import requests
import json
#Setup the body
rawJSON = dict()
rawJSON['appId'] = '<appKey>'
rawJSON['clientKey'] = '<devKey>'
#Base URL
url = "https://www.leanplum.com/api?apiVersion=1.0.6&action=multi"
#Array for each batch of 50
dataValues = []
counter = 1
#Setting batch size to 1 for testing
while(counter < 2):
#Each API call in the batch needs to be in a dictionary
node = dict()
node['action'] = 'track'
node['event'] = '<event>'
node['userId'] = '<userId>'
node['value'] = <numeric Value>
node['currencyCode'] = 'USD'
node['time'] = 1544173994
#Exclude this for production but, you can check debugger to see the API call came through.
node['devMode'] = True
#All API calls are aggregated in an array
dataValues.append(node)
#Increment counter
counter += 1
#Add our API array into the 'data' field
rawJSON['data'] = dataValues
print("JSON Body:")
print(json.dumps(rawJSON))
#Send out the POST request. It's important to use the data parameter not json
resp = requests.post(url,data=json.dumps(rawJSON))
print(resp)
import requests
import json
#Setup the body
rawJSON = dict()
rawJSON['appId'] = '<appKey>'
rawJSON['clientKey'] = '<devKey>'
#Base URL
url = "https://www.leanplum.com/api?apiVersion=1.0.6&action=multi"
#Array for each batch of 50
dataValues = []
counter = 1
#Setting batch size to 1 for testing
while(counter < 2):
#The attributes you want to update need to be stored in a dictionary
#See the setUserAttributes API for possible options
attrDict = dict()
attrDict['<attributeName>'] = '<attribute Value>
#Each API call in the batch needs to be in a dictionary
node = dict()
node['action'] = 'setUserAttributes'
node['userAttributes'] = attrDict
node['userId'] = '<userId>'
#Exclude this for production but, you can check debugger to see the API call came through.
node['devMode'] = True
#All API calls are aggregated in an array
dataValues.append(node)
#Increment counter
counter += 1
#Add our API array into the 'data' field
rawJSON['data'] = dataValues
print("JSON Body:")
print(json.dumps(rawJSON))
#Send out the POST request. It's important to use the data parameter not json
resp = requests.post(url,data=json.dumps(rawJSON))
print(resp)
While Leanplum is processing a successful
multi
call, concurrent requests for these users (via the SDK or API) will be queued and completed after themulti
batch completes.
Response
Each individual action will also have its own response; some may be successes, some may have warnings, and some may have errors. The index of the response will match the index of the request in the data
array. For the example above, if we pass a bad string as the action
in the second batched request, we get the following response:
{
"response": [
{
"success": true
},
{
"success": false,
"error": {
"message": "Action not found"
}
}
]
}