Skip to main content

Examples

These are the same snippets as the send.sh, send.py and send.ino tabs under Send data. In the app they come with your crate's URL filled in, and your key too once you show it.

Replace YOUR_CRATE_ID and YOUR_API_KEY with your own.


curl​

curl -X POST "https://data.carroted.com/api/v2/ingest/YOUR_CRATE_ID" \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{"device": "sensor-1", "metric": "temperature", "value": 22.5}'

Python​

Uses the requests library (pip install requests).

import requests

URL = "https://data.carroted.com/api/v2/ingest/YOUR_CRATE_ID"
KEY = "YOUR_API_KEY"

reading = {"device": "sensor-1", "metric": "temperature", "value": 22.5}
r = requests.post(URL, json=reading, headers={"X-API-Key": KEY}, timeout=10)
print(r.status_code, r.text)

To send several readings in one request, post a list to the batch URL:

URL = "https://data.carroted.com/api/v2/ingest/YOUR_CRATE_ID/batch"

readings = [
{"device": "sensor-1", "metric": "temperature", "value": 22.5},
{"device": "sensor-1", "metric": "humidity", "value": 61},
]
r = requests.post(URL, json=readings, headers={"X-API-Key": KEY}, timeout=10)

ESP32 (Arduino)​

Connects to Wi-Fi and sends a reading every minute. Put your sensor reading where the comment says.

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

const char* WIFI_SSID = "your-wifi";
const char* WIFI_PASS = "your-password";
const char* URL = "https://data.carroted.com/api/v2/ingest/YOUR_CRATE_ID";
const char* KEY = "YOUR_API_KEY";

void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(500);
}

void loop() {
float temperature = 22.5; // read your sensor here
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
http.begin(client, URL);
http.addHeader("Content-Type", "application/json");
http.addHeader("X-API-Key", KEY);
String body = "{\"device\":\"esp32-1\",\"metric\":\"temperature\",\"value\":" + String(temperature) + "}";
Serial.println(http.POST(body));
http.end();
delay(60000);
}

The Serial Monitor prints 200 for each reading that arrives.

note

client.setInsecure() skips checking the server's certificate. That keeps the sketch short; for a device in the field, add the root certificate with client.setCACert().


Anything else​

Any language or tool that can send an HTTPS POST with a JSON body and a header works the same way. See The HTTP API for every field.