The Endpoints CLI is experimental. Commands are prefixed with exp and may change in future releases.
The PostHog CLI provides commands to manage your Endpoints as code. You can version control your endpoint definitions, push changes from local YAML files, and pull existing endpoints for local editing.
# Install the PostHog CLI
npm install -g @posthog/cli
# Authenticate
posthog-cli login
To manage Endpoints with the CLI, your personal API key needs the endpoint:write scope. If your endpoints use variables, you also need the insight_variable:read or insight_variable:write scope, depending on whether you intend to update endpoints using the CLI. You can create or update your API key in Personal API Keys.
List all endpoints in your project:
posthog-cli exp endpoints list
View details of a specific endpoint:
posthog-cli exp endpoints get <endpoint-name>
Execute an endpoint and see the results:
# Run a remote endpoint
posthog-cli exp endpoints run <endpoint-name>
# Run from a local YAML file (without creating the endpoint)
posthog-cli exp endpoints run -f my-endpoint.yaml
# Pass variables
posthog-cli exp endpoints run <endpoint-name> --var customer_id=cust_123
# Output as JSON
posthog-cli exp endpoints run <endpoint-name> --json
Open an endpoint in the PostHog UI:
posthog-cli exp endpoints open <endpoint-name>
Download endpoints from PostHog to local YAML files:
# Pull a specific endpoint
posthog-cli exp endpoints pull my-endpoint
# Pull all endpoints
posthog-cli exp endpoints pull --all
# Pull to a specific directory
posthog-cli exp endpoints pull --all -o ./endpoints/
Push local YAML files to PostHog:
# Push a single file
posthog-cli exp endpoints push my-endpoint.yaml
# Push all YAML files in a directory
posthog-cli exp endpoints push ./endpoints/
# Preview changes without applying
posthog-cli exp endpoints push ./endpoints/ --dry-run
Compare local files with remote endpoints:
posthog-cli exp endpoints diff ./endpoints/
Endpoints are defined in YAML files:
name: daily_active_users
description: Count of unique users per day
# Simple HogQL query
query: |
SELECT
toDate(timestamp) as date,
count(DISTINCT distinct_id) as users
FROM events
WHERE event = '$pageview'
GROUP BY date
ORDER BY date DESC
name: events_by_customer
description: Events for a specific customer
query: |
SELECT event, count() as count
FROM events
WHERE properties.customer_id = {variables.customer_id}
GROUP BY event
variables:
- name: customer_id
type: string
default: "default_customer"
name: hourly_metrics
description: Pre-computed hourly metrics
query: |
SELECT count() FROM events
materialization:
enabled: true
schedule: "1hour" # Options: 5min, 15min, 30min, 1hour, 2hour, 4hour, 6hour, 12hour, 24hour, 7day, 30day
For insight-based endpoints, use query_definition instead of query:
name: signup_funnel
description: User signup funnel
query_definition:
kind: FunnelsQuery
funnelsFilter:
funnelVizType: steps
series:
- kind: EventsNode
event: "$pageview"
name: "Page View"
- kind: EventsNode
event: "signed_up"
name: "Signed Up"
# 1. Pull existing endpoints
posthog-cli exp endpoints pull --all -o ./posthog/endpoints/
# 2. Edit locally, commit to git
git add posthog/endpoints/
git commit -m "feat: add new customer metrics endpoint"
# 3. Preview changes
posthog-cli exp endpoints diff ./posthog/endpoints/
# 4. Push to PostHog
posthog-cli exp endpoints push ./posthog/endpoints/