Skip to main content

Push data into Insights

Insights stores your events, measurements and records as documents in sources. Use the SDK to write to a source from your backend: web shop orders, sensor readings, audit events.

1. Create a source​

Sources are created in the Insights app. The SDK does not create sources.

  1. Open Insights → Sources and create a new source.
  2. Choose the type:
    • Store: the general-purpose source. You add and edit entries in the app, write them through the API (Doc.create, Doc.batchInsert) and upload files, repeatedly. Best default. See Store source.
    • Push: high-volume, asynchronous ingest (push). Only service tokens may write.
    • More types (RabbitMQ, BigQuery, …) are in Source types.
  3. Name it, for example Orders.
  4. Define the field types.
Field typeSends as
stringtext
integer, floatnumber (or numeric string)
booleantrue / false
date-isoISO 8601 string, e.g. 2026-08-14T09:30:00Z
timestamp-seconds, timestamp-millis, timestamp-microsepoch numbers
geo-point"lat,lon", [lon, lat], { lat, lon }, GeoJSON or WKT POINT(lon lat)
listarray

Fields you send that are not defined on the source are dropped silently. Add the field type first.

2. Write one document​

import sdk from '@scenid/cloud-sdk'

const orders = sdk.asAdmin().Insights().Source('Orders')

const { status, result } = await orders.Doc.create({
orderId: 'ORD-2001',
orderedAt: '2026-09-01T10:15:00Z',
country: 'DE',
category: 'Coffee',
product: 'Espresso Beans 1kg',
quantity: 2,
unitPrice: 24.9,
total: 49.8,
status: 'paid',
paymentMethod: 'card',
newCustomer: true
})

console.log(status, result) // 201 { id: '…', result: 'created' }

Rules:

  • The body must be flat. No nested objects (except for the formats list and geo-point).
  • Do not send id or _id.
  • The document id depends on the source's duplicate control. With Field (for example orderId), sending the same key again updates the document, so retries are safe. With the store default (Automatic) every call creates a new document. See Duplicates.

3. Write many documents​

const { result } = await orders.Doc.batchInsert(rows)
// { created: 98, updated: 0, failed: 2, total: 100, errors: [ … ] }
  • Send up to a few hundred documents per call. Split larger sets into chunks.
  • A batch is partially successful: invalid documents are reported in errors, the rest is written.
  • batchInsert([]) throws scenid/empty-batch.
const chunk = (items, size) => Array.from(
{ length: Math.ceil(items.length / size) },
(_, i) => items.slice(i * size, i * size + size)
)

for (const part of chunk(rows, 100)) {
const { status, result } = await orders.Doc.batchInsert(part)
if (status !== 200) throw new Error(`batch failed: ${status}`)
if (result.failed > 0) console.warn(result.errors)
}

4. Streaming ingest with push​

For push sources, use push. It queues documents and answers immediately. They are validated and indexed asynchronously.

const { status, result } = await sdk.asAdmin().Insights().Source('Sensor stream').push([
{ timestamp: new Date().toISOString(), deviceId: 'SENS-HH-01', temperature: 20.4 }
])

console.log(result) // { ok: true, queued: 1 }

Only service tokens may push (sdk.asAdmin()), and only to sources of type push.

Example: report every order from your shop​

shop-hook.mjs
import sdk from '@scenid/cloud-sdk'

const orders = sdk.asAdmin().Insights().Source('Orders')

export const onOrderPaid = async order => {
const { status, result } = await orders.Doc.create({
orderId: order.id,
orderedAt: order.paidAt,
country: order.shipping.country,
category: order.items[0].category,
product: order.items[0].name,
quantity: order.items[0].quantity,
unitPrice: order.items[0].price,
total: order.total,
status: 'paid',
paymentMethod: order.paymentMethod,
newCustomer: order.customer.isNew
})

if (status >= 400) console.error('insights write failed', status, result)
}

Duplicates​

The store's default is a random id per document, so nothing is de-duplicated. In the source settings, set the Document ID to Field and pick a key such as orderId: re-sending an order then updates it. Choose this before the first data arrives, it cannot be changed once documents exist. Push, RabbitMQ and BigQuery sources default to a content hash, where identical documents overwrite each other.