Pby PostHog
Run a SQL query
Runs a SQL (HogQL) query over the project's events, persons and other data and returns the rows.
Description
Over the API, send the SQL as `{"query": {"kind": "HogQLQuery", "query": "..."}}` with a personal API key that has the `query:read` scope. A query returns up to 100 rows by default and up to 50,000 with an explicit `LIMIT`; the endpoint is for ad-hoc analysis, not bulk export.
run-sql-query.md
Run a SQL query
Runs a SQL (HogQL) query over the project's events, persons and other data and returns the rows.
Over the API, send the SQL as {"query": {"kind": "HogQLQuery", "query": "..."}} with a personal API key that has the query:read scope. A query returns up to 100 rows by default and up to 50,000 with an explicit LIMIT; the endpoint is for ad-hoc analysis, not bulk export.
Set up
Use the first option your agent supports.
MCP (official, remote)
Add this server to your agent's MCP settings, then sign in when asked.
{ "mcpServers": { "posthog": { "url": "https://mcp.posthog.com/mcp" } } }
Call the MCP tool execute-sql.
Server URL: https://mcp.posthog.com/mcp
CLI (official)
Install the command, then confirm it runs.
npm install -g @posthog/cli@latest
posthog-cli --version
Run posthog-cli api call execute-sql.
Set $POSTHOG_CLI_API_KEY in your environment first (get a key: https://app.posthog.com/settings/user-api-keys?preset=mcp_server).
API (official)
- Base URL: https://us.posthog.com
- Endpoint:
POST /api/projects/:project_id/query/ - Auth: send the header
Authorization: Bearer $POSTHOG_PERSONAL_API_KEY - Get a key: https://us.posthog.com/settings/user-api-keys
- Docs: https://posthog.com/docs/api
Before doing anything else, make one read-only call to confirm access.
Rules
- Ask the user before anything that sends messages, costs money, or changes data.
- Never print API keys.
Built from these files in the repository
The layout and the rules come from lib/catalog/render-markdown.ts, the set-up wording from lib/catalog/render-access.ts.
Ways in
Workflows using Run a SQL query
TContributed by thedogwiththedataonitFlag high-intent website visitors to salesIdentify promising accounts on your site, enrich them, and tell the right owner with useful context.
P
C
S
TContributed by thedogwiththedataonitGuide active free users toward their first paid momentCombine behavioural milestones with timely education so promising users find the value before momentum fades.
P
SB