# How to Call Posthog API Securely from Mobile App

> Never place POSTHOG_PERSONAL_API_KEY inside Mobile App (iOS & Android) source. RealComposer injects them when calling https://us.posthog.com/api/projects/{{query.projectId}}/feature_flags, so browsers see only a client token.

- Canonical page: https://realcomposer.com/recipes/posthog-mobile-proxy
- Client platform: Mobile App (iOS & Android)
- Upstream services: PostHog
- Category: Security & Secret Vault
- JSON version: https://realcomposer.com/recipes/posthog-mobile-proxy.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=posthog-mobile-proxy&via=agent

## Problem: PostHog keys in Mobile App (iOS & Android) network tabs

Mobile App (iOS & Android) ships code or scripts to end-user devices. Any PostHog bearer token in those bundles can be copied from DevTools, proxies, or repackaged apps.

- Shared Mobile App (iOS & Android) projects revealing PostHog tokens
- Sudden PostHog quota burn from scripted abuse
- Inability to rotate keys without redeploying clients

## Solution steps

1. **Add vault secrets for this pipeline**: Create POSTHOG_PERSONAL_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Configure upstream steps and output mapping**: 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 Mobile App (iOS & Android)**: Allowed origins on the token should list every Mobile App (iOS & Android) host that calls this URL.

```
let url = URL(string: "https://realcomposer.com/v1/workspace/posthog-mobile-proxy")!
var request = URLRequest(url: url)
request.addValue("Bearer rc_live_token", forHTTPHeaderField: "Authorization")
```

## 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": "How to Call Posthog API Securely from Mobile App",
  "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
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "flagKey",
        "sourcePath": "results.0.key",
        "type": "string",
        "sourceStep": "posthog"
      },
      {
        "targetPath": "active",
        "sourcePath": "results.0.active",
        "type": "boolean",
        "sourceStep": "posthog"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/posthog-mobile-proxy" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_posthog-",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "posthog-mobile-proxy"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "flagKey": "sample",
  "active": "sample"
}
```

## FAQ

### Which PostHog credential belongs in the vault for this pipeline?

Yes for posthog-mobile-proxy. Ship only an rc_live_ client token in Mobile App (iOS & Android); RealComposer injects POSTHOG_PERSONAL_API_KEY on https://us.posthog.com/api/projects/{{query.projectId}}/feature_flags.

### What cache key does RealComposer use?

posthog-mobile-proxy vault headers attach server-side, but Anthropic or OpenAI model and messages must come from the client body.

### How do I rotate upstream credentials?

Upstream steps in posthog-mobile-proxy honor timeoutMs and optional fallback JSON without leaking secrets.
