Schema management

Schema management lets you define and enforce the structure of your events using typed property groups. This helps document expected properties, provides type safety in your code, and makes your analytics implementation more maintainable.

Creating events

You can create event definitions before any events are actually captured. This is useful when planning your analytics implementation upfront, allowing you to:

  • Define your event schema before writing instrumentation code
  • Generate typed definitions for events that haven't been captured yet
  • Document expected events for your team
  • Ensure consistency across your codebase from day one

To create an event:

  1. Go to Data Management > Events
  2. Click Create event
  3. Enter the event name (e.g., "user_signed_up")
  4. Optionally add:
    • Description: What the event represents
    • Owner: Team member responsible for this event
    • Tags: Organizational labels
Event names
Event names cannot be changed after creation, so choose carefully.

Events created this way will show "-" for First seen and Last seen until the first event is actually captured by PostHog.

Creating a new event definition

Creating property groups

Property groups are reusable collections of typed properties that can be attached to one or more events.

  1. Navigate to Data Management > Property Groups
  2. Click New Property Group
  3. Give your group a name (e.g., Order Information)
  4. Add properties with their types:
    • Name: The property key
    • Type: String, Number, Boolean, Object, or Array
    • Required: Whether the property must be present
    • Description: Help text for your team
Creating a property group

Property groups can be reused across multiple events, making it easy to maintain consistent schemas.

Adding schemas to events

Once you've created property groups and events (either new events you created, or existing events from captured data), attach property groups to your events:

  1. Go to Data Management > Events
  2. Select an event
  3. In the Schema section, click Add Property Group
  4. Choose from your existing property groups or create a new one
Event schema with property group

Events can have multiple property groups, and each group's properties will be included in the generated types.

Downloading your schema with the CLI

hog, the PostHog CLI, generates language definitions from your configured schemas.

Install the CLI

Terminal
curl -fsSL https://posthog.com/install.sh | sh

See the CLI docs for Windows, Homebrew, and npm.

Authenticate

Terminal
hog auth login

This opens your browser. Check that the code matches the one in your terminal, select your organization and project, set the permissions to grant, and click Approve.

hog CLI authorization page

Download your schema

Terminal
hog schema pull --language typescript

The CLI will:

  1. Fetch your event schemas
  2. Prompt you for the output file path, unless you pass --file
  3. Generate typed definitions and save them to the specified file
  4. Update posthog.json with the language, file path, and schema hash

--language takes typescript, golang, or python. Later runs of hog schema pull without --language regenerate every language in posthog.json.

When prompted for the output path, choose a location that's accessible from your application code (e.g., src/lib/posthog-typed.ts or app/lib/posthog-typed.ts for TypeScript).

Important: Commit both posthog.json and posthog-typed.<extension> to your git repository. This ensures your team has consistent types and tracks schema changes over time.

On subsequent runs, the CLI only updates the file if your schema has changed.

TypeScript SDK options

By default, schema pull for TypeScript generates a module that wraps posthog-js – you import it instead of posthog-js and get type-checked capture() calls. If your app uses a different SDK (e.g. posthog-react-native or posthog-node), pass --sdk none to generate only the event type definitions with no SDK import:

Terminal
hog schema pull --language typescript --sdk none

FlagOutput fileWhat it generates
(default)posthog-typed.tsA typed posthog-js wrapper with capture() and captureRaw()
--sdk noneposthog-events.tsStandalone types: EventSchemas, EventName, and EventProperties

The CLI saves the sdk setting in posthog.json, so subsequent runs remember your choice.

Check schema status

Terminal
hog schema status

This shows each generated file, whether it matches your schemas in PostHog, the number of events, and when it was last updated. It exits with code 1 when a generated file is missing or out of date, so you can run it in CI.

Using typed events in your app

Once you've downloaded your schema, import and use the generated types.

posthog-js (default)

The default TypeScript output wraps posthog-js with type-safe capture() and captureRaw() methods:

typescript
import posthog from "./posthog-typed";
// Use typed events with autocomplete and type safety on known events:
posthog.capture("button_clicked", {
button_name: "signup", // ✓ Type-checked
click_count: 5, // ✓ Type-checked
// missing_required: ... // ✗ TypeScript error if required
});
// Use captureRaw() when you need to bypass type checking:
posthog.captureRaw("dynamic_event", { whatever: "data" });

Types only (--sdk none)

With --sdk none, the generated posthog-events.ts file exports only the event types. Import them and use them with any PostHog SDK:

typescript
import type { EventName, EventProperties } from "./posthog-events";
import PostHog from "posthog-react-native";
// Create a typed wrapper around any SDK's capture method:
function track<K extends EventName>(event: K, properties: EventProperties<K>) {
posthog.capture(event, properties);
}
track("button_clicked", {
button_name: "signup", // ✓ Type-checked
click_count: 5, // ✓ Type-checked
});

Go

The Go package that schema pull generates follows the posthog-go version in your go.mod. To choose it yourself, pass --sdk posthog-go for 1.x (github.com/posthog/posthog-go) or --sdk posthog-go-v2 for 2.x (github.com/posthog/posthog-go/v2):

Terminal
hog schema pull --language golang --sdk posthog-go-v2

The example below uses posthog-go 2.x. With the 1.x package, import github.com/posthog/posthog-go instead.

Go
import (
"github.com/posthog/posthog-go/v2"
typed "your-project/posthog-typed"
)
func captureEventWithRequiredProperties(client posthog.Client) error {
return client.Enqueue(typed.DashboardModeToggledCapture("distinct_id", 2048,
typed.DashboardModeToggledWithFileName("file_name"),
typed.DashboardModeToggledWithFileType(".pdf"),
typed.DashboardModeToggledWithExtraProps(posthog.Properties{
"extra": "property",
}),
))
}
func captureEventWithNoRequiredProperties(client posthog.Client) error {
return client.Enqueue(typed.DashboardLoadingTimeCapture("distinct_id", posthog.Properties{
"test": "property",
"two_and_two_is": 4,
}))
}
func captureEventWithFullCaptureControl(client posthog.Client) error {
return client.Enqueue(typed.DashboardLoadingTimeCaptureFromBase(
// Event should be omitted here as it will be retrieved from the typed function.
posthog.Capture{
DistinctId: "distinct_id",
Timestamp: time.Now(),
// ... Any additional properties
},
posthog.Properties{
"test": "property",
},
))
}

Python

Python
import os
from posthog_typed import PosthogTyped
def main():
posthog = PosthogTyped(
POSTHOG_API_KEY,
host=POSTHOG_HOST,
)
# Capture with required properties
posthog.capture_downloaded_file(file_size_b=2048, file_name="file_name", distinct_id="distinct_id",
extra_properties={"test": "property"})
# Capture with no required properties
posthog.capture_user_logged_in(extra_properties={"test": "property"}, distinct_id="distinct_id")
# Base captures are still possible
posthog.capture("capture_raw_example", distinct_id="distinct_id", properties={"test": "property"})

The generated typed file provides:

  • Type safety: Properties are validated against your schema
  • Autocomplete: Your IDE suggests event names and properties
  • Documentation: Inline types show required properties and descriptions
  • Flexibility: The standard SDK functionality remains available (capture / identify / experiments / ...), allowing gradual migrations

TypeScript type checking in action

Best practices

  • Define events upfront: Create event definitions before implementing instrumentation to get type safety from day one
  • Start with your most important events: Define schemas for critical conversion events first
  • Use descriptive property groups: Name groups by their purpose (e.g., "E-commerce Properties", "User Context")
  • Don't over-schema: Not every event needs a schema—use them where type safety adds value
  • Commit generated types: Always commit posthog.json and posthog-typed.<extension> to version control so your team stays in sync

Still have questions?

Was this page useful?