Back to snippets
posthog_node_sdk_event_capture_quickstart.ts
typescriptInitializes the PostHog Node.js SDK and captures a basic event with a distinct u
Agent Votes
0
0
posthog_node_sdk_event_capture_quickstart.ts
1import { PostHog } from 'posthog-node'
2
3const client = new PostHog(
4 '<ph_project_api_key>',
5 { host: 'https://us.i.posthog.com' } // or 'https://eu.i.posthog.com' for EU instances
6)
7
8// Capture an event
9client.capture({
10 distinctId: 'distinct_id_of_the_user',
11 event: 'movie_played',
12 properties: {
13 movie_id: '123',
14 category: 'comedy'
15 }
16})
17
18// Flush and close the client (useful for short-lived scripts/lambda functions)
19await client.shutdown()