|
| 1 | +# Connecting Twig to a Local PostHog Instance |
| 2 | + |
| 3 | +This guide walks you through running Twig's dev build against a local PostHog instance (localhost:8010). |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- A running local PostHog instance at `http://localhost:8010` ([PostHog local development docs](https://posthog.com/handbook/engineering/developing-locally)) |
| 8 | +- Node.js 22+ |
| 9 | +- pnpm 10+ |
| 10 | + |
| 11 | +## 1. Set up the OAuth application in PostHog |
| 12 | + |
| 13 | +Twig authenticates with PostHog via OAuth. Your local PostHog instance needs an OAuth application registered for Twig to connect to it. |
| 14 | + |
| 15 | +### Option A: Generate demo data (easiest) |
| 16 | + |
| 17 | +PostHog's demo data generator creates a pre-configured OAuth application with the correct client ID: |
| 18 | + |
| 19 | +```bash |
| 20 | +# In your PostHog repo |
| 21 | +python manage.py generate_demo_data |
| 22 | +``` |
| 23 | + |
| 24 | +This creates an OAuth application with: |
| 25 | +- **Client ID**: `DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ` |
| 26 | +- **Redirect URIs**: includes `http://localhost:8237/callback` and `http://localhost:8239/callback` |
| 27 | + |
| 28 | +### Option B: Create the OAuth application manually via Django admin |
| 29 | + |
| 30 | +1. Go to http://localhost:8010/admin/posthog/oauthapplication/ |
| 31 | +2. Click **Add OAuth Application** |
| 32 | +3. Set these fields: |
| 33 | + - **Name**: `Twig` (or whatever you like) |
| 34 | + - **Client ID**: `DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ` — this must match the `POSTHOG_DEV_CLIENT_ID` in Twig's source |
| 35 | + - **Client type**: `Public` (Twig is an Electron desktop app) |
| 36 | + - **Authorization grant type**: `Authorization code` |
| 37 | + - **Redirect URIs**: `http://localhost:8237/callback http://localhost:8239/callback` |
| 38 | + - **Algorithm**: `RS256` |
| 39 | +4. Save |
| 40 | + |
| 41 | +> **Important**: The Client ID must be exactly `DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ` — this is hardcoded in Twig as the Dev region client ID (see `apps/twig/src/shared/constants/oauth.ts`). |
| 42 | +
|
| 43 | +## 2. Configure RSA keys in PostHog |
| 44 | + |
| 45 | +OAuth token signing requires an RSA private key. In your PostHog repo: |
| 46 | + |
| 47 | +```bash |
| 48 | +# Copy the RSA key from .env.example to your .env |
| 49 | +grep OIDC_RSA_PRIVATE_KEY .env.example >> .env |
| 50 | +``` |
| 51 | + |
| 52 | +Or generate a new one: |
| 53 | + |
| 54 | +```bash |
| 55 | +openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform PEM | \ |
| 56 | + awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' |
| 57 | + |
| 58 | +# Add to your PostHog .env as OIDC_RSA_PRIVATE_KEY="<generated_key>" |
| 59 | +``` |
| 60 | + |
| 61 | +## 3. Clone and run Twig |
| 62 | + |
| 63 | +```bash |
| 64 | +git clone https://github.com/PostHog/Twig.git |
| 65 | +cd Twig |
| 66 | +pnpm install |
| 67 | +cp .env.example .env |
| 68 | +pnpm dev |
| 69 | +``` |
| 70 | + |
| 71 | +## 4. Connect to your local instance |
| 72 | + |
| 73 | +1. When the Twig app opens, select the **Dev** region on the login screen (in addition to US & EU, the dev build shows a Dev option that points to `localhost:8010`) |
| 74 | +2. This will redirect you to your local PostHog instance for OAuth authorization |
| 75 | +3. Authorize the application and select the project/organization access level |
| 76 | +4. You'll be redirected back to Twig, now connected to your local PostHog |
| 77 | + |
| 78 | +## How it works |
| 79 | + |
| 80 | +The dev build of Twig includes a "Dev" cloud region that maps to: |
| 81 | +- **API URL**: `http://localhost:8010` |
| 82 | +- **OAuth Client ID**: `DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ` |
| 83 | + |
| 84 | +This is defined in `apps/twig/src/shared/constants/oauth.ts`. The Dev region only appears when running the dev build (`pnpm dev`), not in production releases. |
| 85 | + |
| 86 | +## Troubleshooting |
| 87 | + |
| 88 | +### "Invalid client_id" error during OAuth |
| 89 | + |
| 90 | +The OAuth application in your local PostHog must have the client ID `DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ`. Verify at http://localhost:8010/admin/posthog/oauthapplication/. |
| 91 | + |
| 92 | +### "Redirect URI mismatch" |
| 93 | + |
| 94 | +Make sure the OAuth application's redirect URIs include `http://localhost:8237/callback` and `http://localhost:8239/callback`. Check for trailing slashes. |
| 95 | + |
| 96 | +### OAuth authorization page fails to load |
| 97 | + |
| 98 | +Ensure your local PostHog instance is running at `http://localhost:8010` and that the RSA key is configured (see step 2). |
| 99 | + |
| 100 | +### Existing projects not showing up |
| 101 | + |
| 102 | +After connecting, Twig will show projects from your local PostHog instance. If you need test data, run `python manage.py generate_demo_data` in your PostHog repo. |
| 103 | + |
| 104 | +## Further reading |
| 105 | + |
| 106 | +- [PostHog OAuth Development Guide](https://github.com/PostHog/posthog/blob/master/docs/published/handbook/engineering/oauth-development-guide.md) — full OAuth spec, scopes, token introspection, and more |
0 commit comments