How-to guideobservability6–8 minCookbook
Node.js Example: Sending Logs to IngestAPI
Drop-in Node.js example for sending JSON logs to TG2G.
Last updated 2025-11-26
nodejslogsingest
Share:
Install dependencies
You can use the built-in `https` module or a library like `node-fetch` or `axios`. The example below uses `node-fetch` for readability.
Install node-fetch
npm install node-fetch@2Send a batch of logs
This example sends two log entries to the IngestAPI with a shared source and entity.
Node.js logging example
const fetch = require('node-fetch');
async function sendLogs() {
const payload = {
source: "checkout-service",
entity: {
name: "checkout-service",
kind: "service",
tags: ["prod", "payments"]
},
logs: [
{
timestamp: new Date().toISOString(),
level: "error",
message: "Payment gateway timeout",
requestId: "req-" + Date.now(),
region: "us-east-1"
}
]
};
const res = await fetch("https://ingest.techguys2go.com/v1/logs", {
method: "POST",
headers: {
"X-TG2G-Api-Key": process.env.TG2G_INGEST_KEY,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
if (!res.ok) {
console.error("Failed to send logs:", res.status, await res.text());
}
}
sendLogs().catch(console.error);