// Public documentation sample: no WingsFlow imports, sessions or DB access. export async function integrate( base, key, { channelId, templateId, externalId = 'customer-842', fetchImpl = fetch, } = {}, ) { const send = async (method, path, body, idem) => { const response = await fetchImpl(base + path, { method, headers: { Authorization: `Bearer ${key}`, ...(body ? { 'Content-Type': 'application/json', 'Idempotency-Key': idem } : {}), }, ...(body ? { body: JSON.stringify(body) } : {}), }); const json = await response.json(); if (!response.ok) throw new Error( `${response.status} ${json.error?.code} ${json.error?.request_id}`, ); return json.data; }; const me = await send('GET', '/me'), channels = await send('GET', '/channels'); channelId ??= channels[0]?.id; if (!channelId) throw new Error('No granted channel; ask administrator'); const templates = await send('GET', '/templates?channel_id=' + channelId); templateId ??= templates[0]?.id; const selected = templates.find((t) => t.id === templateId); if (!selected) throw new Error('No granted approved template'); const contact = await send( 'PUT', '/contact-identities/' + encodeURIComponent(externalId), { phone: '+919876543210', name: 'Documentation fixture' }, externalId + '-profile-v1', ); const event = await send( 'POST', '/events', { source_event_id: externalId + ':order:ORD-123:created:v1', event: 'order.created', schema_version: '1', occurred_at: '2026-09-14T06:30:00Z', contact_id: contact.id, data: { order_id: 'ORD-123', aggregate_version: 1, amount_minor: 2500000, currency: 'INR', }, correlation_id: 'order:ORD-123', }, externalId + '-order-created-v1', ); return { tenantId: me.tenant_id, contact, event, channelId, templateId }; }