Skip to content

Instantly share code, notes, and snippets.

@knorwood
Created September 5, 2018 01:30
Show Gist options
  • Save knorwood/574602478e9d4c346c564eba60955636 to your computer and use it in GitHub Desktop.
Save knorwood/574602478e9d4c346c564eba60955636 to your computer and use it in GitHub Desktop.
Sending data to Amplitude's Batch Endpoint
"""
Sending a payload of events to the /batch endpoint using the requests library.
"""
import json
import requests
# Be cautious when setting your API key and running this script since it will send the following
# two events to your app!
API_KEY = 'YOUR_API_KEY_HERE'
EVENTS = [
{'user_id': 'Alice', 'event_type': 'hello world'},
{'user_id': 'Bob', 'event_type': 'hello world'},
]
def send_events_easy_way():
print "Sending events the easy way"
payload = {
"api_key": API_KEY,
"events": EVENTS,
"options": {
"min_id_length": 0,
},
}
response = requests.post(
'https://api.amplitude.com/batch',
json=payload
)
print "Status:", response.status_code
print "Response:", response.content
def send_events_explicitly_set_content_type():
print "Sending events while explicitly setting the content type header"
payload = {
"api_key": API_KEY,
"events": EVENTS,
"options": {
"min_id_length": 0,
},
}
headers = {'Content-Type': 'application/json'}
response = requests.post(
'https://api.amplitude.com/batch',
data=json.dumps(payload),
headers=headers
)
print "Status:",response.status_code
print "Response:", response.content
def _main():
send_events_easy_way()
send_events_explicitly_set_content_type()
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment