SDK DATABASE untuk akses Database Cloudku
npm install @xstbot/cloudku-databaseCONTOH PENGGUNAAN LENGKAP API
1. Spesifikasi API
Endpoint : https://db.cloudku.sbs/api/db/cloudku/sbs
Method : POST
Header : Content-Type: application/json
Fungsi : Menyimpan data JSON apa pun (user, sensor, log, dll) ke database.
2. Contoh 1 — Kirim Data User
2.1 Tujuan : Menyimpan data pengguna aplikasi.
2.2 cURL
curl -X POST https://db.cloudku.sbs/api/db/cloudku/sbs \
-H "Content-Type: application/json" \
-d '{
"type": "user",
"username": "dev",
"role": "admin",
"created_at": "2025-01-01 10:00:00"
}'
3. Contoh 2 — Kirim Data Sensor IoT
3.1 Tujuan : Menyimpan data pembacaan sensor.
3.2 cURL
curl -X POST https://db.cloudku.sbs/api/db/cloudku/sbs \
-H "Content-Type: application/json" \
-d '{
"type": "sensor",
"device_id": "ESP32-01",
"sensor": "DHT22",
"temperature": 24.5,
"humidity": 60,
"status": "OK",
"timestamp": "2025-01-01 10:05:00"
}'
4. Contoh 3 — Penggunaan di ESP32 (Arduino)
#include
#include
const char* ssid = "NAMA_WIFI";
const char* password = "PASSWORD_WIFI";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://db.cloudku.sbs/api/db/cloudku/sbs");
http.addHeader("Content-Type", "application/json");
String jsonData = R"rawliteral(
{
"type": "sensor",
"device_id": "ESP32-01",
"sensor": "DHT22",
"temperature": 25.1,
"humidity": 58,
"status": "OK"
}
)rawliteral";
int httpResponseCode = http.POST(jsonData);
Serial.println(httpResponseCode);
http.end();
}
}
void loop() {
}
5. Contoh 4 — Penggunaan di Node.js
const axios = require("axios");
axios.post("https://db.cloudku.sbs/api/db/cloudku/sbs", {
type: "sensor",
device_id: "SERVER-01",
cpu_usage: 65,
ram_usage: 70,
status: "NORMAL"
})
.then(res => {
console.log("Success:", res.data);
})
.catch(err => {
console.error("Error:", err.message);
});
6. Contoh 5 — Penggunaan di Python
import requests
url = "https://db.cloudku.sbs/api/db/cloudku/sbs"
data = {
"type": "user",
"username": "operator1",
"role": "operator"
}
response = requests.post(url, json=data)
print(response.json())