# Merge Clerk User Profile with Posthog Feature Flags

> Instead of Frontend / Web firing serial fetches to PostHog and Clerk, one pipeline hits https://us.posthog.com/api/projects/{{query.projectId}}/feature_flags (and sibling steps) in parallel and returns mapped JSON.

- Canonical page: https://realcomposer.com/recipes/merge-clerk-and-posthog-feature-flags
- Client platform: Frontend / Web
- Upstream services: PostHog, Clerk
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/merge-clerk-and-posthog-feature-flags.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=merge-clerk-and-posthog-feature-flags&via=agent

## Problem: Waterfall latency from multiple PostHog and Clerk requests

Frontend / Web widgets often await PostHog and Clerk sequentially, multiplying TLS handshakes on cellular networks and amplifying failure rates.

- Race conditions between parallel manual fetches
- Higher mobile data usage from redundant JSON
- Timeouts when any upstream exceeds cellular RTT

## Solution steps

1. **Add vault secrets for this pipeline**: Store POSTHOG_PERSONAL_API_KEY, CLERK_SECRET_KEY in the vault. Frontend / Web never embeds the raw secret, only a revocable rc_live_ client token.

2. **Target the real upstream URL**: Use GET against https://us.posthog.com/api/projects/{{query.projectId}}/feature_flags. Allowed origins on the client token should match leave cache ttl at 0 when each response is user-specific (include userid or record id in query params before enabling cache).

3. **Call from Frontend / Web**: Call https://realcomposer.com/v1/workspace/merge-clerk-and-posthog-feature-flags with a restricted rc_live_ client token. Frontend / Web never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/merge-clerk-and-posthog-feature-flags', {
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
```

## Pipeline config

Import this config into a RealComposer pipeline. `{{secrets.NAME}}` values are read from the encrypted Secret Vault at request time, so the client never sees them.

```json
{
  "name": "Merge Clerk User Profile with Posthog Feature Flags",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "posthog",
      "upstreamUrlTemplate": "https://us.posthog.com/api/projects/{{query.projectId}}/feature_flags",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.POSTHOG_PERSONAL_API_KEY}}"
      },
      "timeoutMs": 3000
    },
    {
      "stepKey": "clerk",
      "upstreamUrlTemplate": "https://api.clerk.com/v1/users/{{query.userId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.CLERK_SECRET_KEY}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "posthog.flagKey",
        "sourcePath": "results.0.key",
        "type": "string",
        "sourceStep": "posthog"
      },
      {
        "targetPath": "posthog.active",
        "sourcePath": "results.0.active",
        "type": "boolean",
        "sourceStep": "posthog"
      },
      {
        "targetPath": "clerk.userId",
        "sourcePath": "id",
        "type": "string",
        "sourceStep": "clerk"
      },
      {
        "targetPath": "clerk.email",
        "sourcePath": "email_addresses.0.email_address",
        "type": "string",
        "sourceStep": "clerk"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/merge-clerk-and-posthog-feature-flags" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_merge-cl",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "merge-clerk-and-posthog-feature-flags"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "posthog_flagKey": "sample",
  "posthog_active": "sample",
  "clerk_userId": "sample",
  "clerk_email": "sample"
}
```

## FAQ

### Is it safe to log PostHog responses in Frontend / Web?

Use scoped tokens where the vendor allows them on merge-clerk-and-posthog-feature-flags. Admin keys stay {{secrets.*}} references only.

### When does edge caching apply to this recipe?

merge-clerk-and-posthog-feature-flags POST steps bypass caches.default even if cacheTtlSeconds is set in the dashboard.

### What happens if an upstream step times out?

Upstream steps in merge-clerk-and-posthog-feature-flags honor timeoutMs and optional fallback JSON without leaking secrets.
