Initialization
You initialize the three Grounded SDKs independently because they serve different runtimes and different jobs.
@grounded/client
import { Grounded } from '@grounded/client';
const grounded = new Grounded({
apiKey: process.env.GROUNDED_SECRET_KEY!,
baseUrl: process.env.GROUNDED_BASE_URL ?? 'http://localhost:3001',
timeoutMs: 30_000,
maxRetries: 2,
});Options
| Option | Required | Default | Meaning |
|---|---|---|---|
apiKey | yes | none | secret API key |
baseUrl | no | http://localhost:3001 | Grounded origin or origin plus /api |
timeoutMs | no | 30000 | per-request timeout |
maxRetries | no | 2 | retries for 429 and 5xx responses |
fetch | no | global fetch | custom fetch implementation |
@grounded/tracing
import { GroundedTracing } from '@grounded/tracing';
const tracing = new GroundedTracing({
publishableKey: process.env.GROUNDED_PUBLISHABLE_KEY!,
agentId: 'agt_123',
baseUrl: process.env.GROUNDED_BASE_URL ?? 'http://localhost:3001',
environment: 'production',
release: 'api-2026-03-09',
flushAt: 20,
flushIntervalMs: 5_000,
sampleRate: 1,
timeoutMs: 15_000,
maxRetries: 2,
});Options
| Option | Required | Default | Meaning |
|---|---|---|---|
publishableKey or apiKey | yes | none | publishable tracing key |
agentId | no | none | default agent for all traces |
baseUrl | no | http://localhost:3001 | Grounded origin or origin plus /api |
environment | no | none | attached to trace.resource.environment |
release | no | none | attached to trace.resource.release |
flushAt | no | 20 | flush after this many completed traces are queued |
flushIntervalMs | no | 5000 | background flush cadence |
sampleRate | no | 1 | fraction of traces to keep |
timeoutMs | no | 15000 | per-flush timeout |
maxRetries | no | 2 | retries for 429, 5xx, and retryable network errors |
fetch | no | global fetch | custom fetch implementation |
mask | no | none | transform a trace before it is sent |
@grounded/analytics
import { init } from '@grounded/analytics';
const analytics = init({
publishableKey: process.env.NEXT_PUBLIC_GROUNDED_PUBLISHABLE_KEY!,
agentId: 'agt_123',
baseUrl: process.env.NEXT_PUBLIC_GROUNDED_BASE_URL ?? 'http://localhost:3001',
flushAt: 20,
flushIntervalMs: 5_000,
capturePageViews: true,
mirror: { fullstory: window.FS },
});Options
| Option | Required | Default | Meaning |
|---|---|---|---|
publishableKey or apiKey | yes | none | publishable telemetry key |
agentId | yes | none | target agent for telemetry |
baseUrl | no | http://localhost:3001 | Grounded origin or origin plus /api |
flushAt | no | 20 | flush after this many queued events |
flushIntervalMs | no | 5000 | background flush cadence |
timeoutMs | no | 15000 | per-flush timeout |
maxRetries | no | 2 | retries for 429, 5xx, and retryable network errors |
fetch | no | global fetch | custom fetch implementation |
storage | no | localStorage or memory fallback | persistence for sessionId, anonymousId, userId |
capturePageViews | no | false | call page() on init |
mirror.fullstory | no | none | forward identify and track calls to FullStory |
Base URL Normalization
All SDK requests target the existing /api routes.
These inputs are equivalent:
baseUrl: 'http://localhost:3001'
baseUrl: 'http://localhost:3001/api'Initialization Patterns
Long-Running API Server
const grounded = new Grounded({
apiKey: process.env.GROUNDED_SECRET_KEY!,
});
const tracing = new GroundedTracing({
publishableKey: process.env.GROUNDED_PUBLISHABLE_KEY!,
agentId: 'agt_123',
environment: process.env.NODE_ENV,
release: process.env.GIT_SHA,
});Browser App
const analytics = init({
publishableKey: process.env.NEXT_PUBLIC_GROUNDED_PUBLISHABLE_KEY!,
agentId: 'agt_123',
capturePageViews: true,
});Worker Or Batch Script
const tracing = new GroundedTracing({
publishableKey: process.env.GROUNDED_PUBLISHABLE_KEY!,
agentId: 'agt_123',
flushIntervalMs: 0,
});
try {
await runBatch();
} finally {
await tracing.shutdown();
}What Happens Under The Hood
Grounded
- adds
Authorization: Bearer ... - adds
Idempotency-KeyonPOST - retries
429and5xx - normalizes list route shapes
GroundedTracing
- validates that each trace can join Grounded's session model
- batches completed traces
- applies sampling before enqueue
- injects
environmentandrelease - sends to
/api/traces/batch
GroundedAnalytics
- persists
sessionIdandanonymousId - batches browser events
- sends to
/api/telemetry/batch - returns correlation context through
getContext()