Use this guide to get started with the Amplitude HTTP V2 API. For a more in-depth look at more considerations and error handling, refer to the complete HTTP V2 API Reference.
For EU data residency, configure the project inside Amplitude EU. Replace the standard endpoint https://api2.amplitude.com/2/httpapi
with the EU residency endpoint https://api.eu.amplitude.com/2/httpapi
in all examples to ensure proper data residency.
Choose your target platform to send a POST request to https://api2.amplitude.com/2/httpapi
(or https://api.eu.amplitude.com/2/httpapi
for EU residency). Replace YOUR_API_KEY
with the API KEY for your Amplitude project.
1curl -X POST https://api2.amplitude.com/2/httpapi \ 2 -H "Content-Type: application/json" \ 3 -H "Accept: */*" \ 4 -d "{ 5 'api_key': 'YOUR_API_KEY', 6 'events': [{ 7 'user_id': '203201202', 8 'device_id': 'C8F9E604-F01A-4BD9-95C6-8E5357DF265D', 9 'event_type': 'watch_tutorial'10 }]11 }"
1wget --method=POST https://api2.amplitude.com/2/httpapi \ 2 --header="Content-Type: application/json" \ 3 --header="Accept: */*" \ 4 --body-data='{ 5 "api_key": "YOUR_API_KEY", 6 "events": [{ 7 "user_id": "203201202", 8 "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D", 9 "event_type": "watch_tutorial"10 }]11 }'
1POST https//api2.amplitude.com/2/httpapi HTTP/1.1 2Host: api2.amplitude.com 3Content-Type: application/json 4Accept: */* 5Body: { 6 "api_key": "YOUR_API_KEY", 7 "events": [{ 8 "user_id": "203201202", 9 "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D",10 "event_type": "watch_tutorial"11 }]12 }
1const payload = { 2 api_key: 'YOUR_API_KEY', // Replace with your Amplitude API key 3 events: [ 4 { 5 'user_id': '203201202', 6 'device_id': 'C8F9E604-F01A-4BD9-95C6-8E5357DF265D', 7 'event_type': 'watch_tutorial', 8 } 9 ]10};11 12async function sendAmplitudeEvent() {13 try {14 const response = await fetch('https://api2.amplitude.com/2/httpapi', {15 method: 'POST',16 headers: {17 'Content-Type': 'application/json'18 },19 body: JSON.stringify(payload)20 });21 22 if (response.code != 200) {23 throw new Error(`HTTP error! status: ${response.status}`);24 }25 26 const data = await response.json();27 console.log('Response:', data);28 } catch (error) {29 console.error('Error:', error);30 }31}32 33// Call the function to send the event34sendAmplitudeEvent();
1fetch('https://api2.amplitude.com/2/httpapi', { 2 method: 'POST', 3 headers: { 4 'Content-Type': 'application/json', 5 'Accept': '*/*' 6 }, 7 body: JSON.stringify({ 8 "api_key": "YOUR_API_KEY", 9 "events": [{10 "user_id": "203201202",11 "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D",12 "event_type": "watch_tutorial"13 }]14 })15})16 .then(response => response.json())17 .then(data => console.log(data))18 .catch(error => console.error(error));
1require "net/http" 2require "json" 3 4url = URI("https://api2.amplitude.com/2/httpapi") 5 6headers = { 7"Content-Type" => "application/json", 8"Accept" => "*/*", 9}10 11data = {12"api_key": "YOUR_API_KEY",13"events": [{14 "user_id": "203201202",15 "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D",16 "event_type": "watch_tutorial",17}],18}.to_json19 20http = Net::HTTP.new(url.host, url.port)21http.use_ssl = true22 23request = Net::HTTP::Post.new(url, headers)24request.body = data25 26response = http.request(request)27 28if response.code == "200"29puts "Success: #{response.body}"30else31puts "Error: #{response.body}"32end
1import requests 2import json 3 4headers = { 5 'Content-Type': 'application/json', 6 'Accept': '*/*' 7} 8 9data = {10 "api_key": "YOUR_API_KEY",11 "events": [{12 "user_id": "203201202",13 "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D",14 "event_type": "watch_tutorial"15 }]16}17 18response = requests.post('https://api2.amplitude.com/2/httpapi',19 headers=headers, data=json.dumps(data))20 21if response.status_code == 200:22 print("Success:", response.json())23else:24 print("Error:", response.text)
AmplitudeEventsSender.java
. Run javac AmplitudeEventsSender.java
in the same directory as where you save it. Then run java AmplitudeEventsSender
to run the program.
1import java.io.BufferedReader; 2import java.io.DataOutputStream; 3import java.io.InputStreamReader; 4import java.net.HttpURLConnection; 5import java.net.URL; 6 7public class AmplitudeEventsSender { 8 9 private final String API_KEY = "YOUR_API_KEY";10 private final String API_URL = "https://api2.amplitude.com/2/httpapi";11 12 public static void main(String[] args) throws Exception {13 AmplitudeEventsSender sender = new AmplitudeEventsSender();14 sender.sendEvents();15 }16 17 private void sendEvents() throws Exception {18 URL url = new URL(API_URL);19 HttpURLConnection con = (HttpURLConnection) url.openConnection();20 21 con.setRequestMethod("POST");22 con.setRequestProperty("Content-Type", "application/json");23 con.setRequestProperty("Accept", "*/*");24 con.setDoOutput(true);25 26 String json = "{\"api_key\": \"" + API_KEY + "\"," +27 "\"events\":[{\"user_id\":\"203201202\"," +28 "\"device_id\":\"C8F9E604-F01A-4BD9-95C6-8E5357DF265D\"," +29 "\"event_type\":\"watch_tutorial\"}]}";30 31 DataOutputStream wr = new DataOutputStream(con.getOutputStream());32 wr.writeBytes(json);33 wr.flush();34 wr.close();35 36 int responseCode = con.getResponseCode();37 System.out.println("Response Code : " + responseCode);38 39 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));40 String inputLine;41 StringBuilder response = new StringBuilder();42 while ((inputLine = in.readLine()) != null) {43 response.append(inputLine);44 }45 in.close();46 47 System.out.println(response.toString());48 }49}
amplitude.go
and run the program by the command go run amplitude.go
.
1package main 2 3import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9)10 11func main() {12 // Define API endpoint13 url := "https://api2.amplitude.com/2/httpapi"14 15 // Define the request body in JSON format16 requestBody := map[string]interface{}{17 "api_key": "YOUR_API_KEY",18 "events": []interface{}{19 map[string]interface{}{20 "user_id": "203201202",21 "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D",22 "event_type": "watch_tutorial",23 },24 },25 }26 27 // Convert the request body to a JSON string28 requestBytes, err := json.Marshal(requestBody)29 if err != nil {30 fmt.Println(err)31 return32 }33 34 // Create a new HTTP request and set the content type35 req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBytes))36 req.Header.Set("Content-Type", "application/json")37 38 // Send the request and get the response39 client := &http.Client{}40 res, err := client.Do(req)41 if err != nil {42 fmt.Println(err)43 return44 }45 defer res.Body.Close()46 47 // Read the response body48 responseBody, err := ioutil.ReadAll(res.Body)49 if err != nil {50 fmt.Println(err)51 return52 }53 54 // Print the response body55 fmt.Println(string(responseBody))56}
After you send data to Amplitude, use one of the debugging tools to check your instrumentation and validate your events.
Thanks for your feedback!
August 23rd, 2024
Need help? Contact Support
Visit Amplitude.com
Have a look at the Amplitude Blog
Learn more at Amplitude Academy
© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.