Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save knorwood/7808dc01c74f83bfb2581f0cf9d0a7da to your computer and use it in GitHub Desktop.
Save knorwood/7808dc01c74f83bfb2581f0cf9d0a7da to your computer and use it in GitHub Desktop.
Sending data to Amplitude's HTTP API (Example)
import requests
import json
import urllib
# 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'},
]
serialized_events = json.dumps(events)
urlencoded_events = urllib.quote(serialized_events)
# HTTP GET
# Explicitly construct query string with api_key and urlencoded events
response = requests.get(
'https://api.amplitude.com/httpapi?api_key={api_key}&event={events}'.format(
api_key=API_KEY, events=urlencoded_events
)
)
print response.content
print response.status_code
# HTTP POST
# Explicitly construct query string with api_key and urlencoded events
response = requests.post(
'https://api.amplitude.com/httpapi',
data='api_key={api_key}&event={events}'.format(
api_key=API_KEY, events=urlencoded_events
)
)
print response.content
print response.status_code
# HTTP POST
# Give json encoded events to library and let it handle urlencoding for you.
response = requests.post(
'https://api.amplitude.com/httpapi',
data={
'api_key': API_KEY,
'event': json.dumps(events),
}
)
print response.content
print response.status_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment