# Combine intercom with posthog user events

> Intercom conversation list plus PostHog person properties in one payload. For "combine intercom with posthog user events", store INTERCOM_ACCESS_TOKEN, POSTHOG_PERSONAL_API_KEY in Secret Vault, call https://api.intercom.io/contacts from a Worker, and let React hit only https://realcomposer.com/v1/workspace/combine-intercom-with-posthog-events with an rc_live_ client token.

- Canonical page: https://realcomposer.com/recipes/combine-intercom-with-posthog-events
- Client platform: React
- Upstream services: Intercom, PostHog
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/combine-intercom-with-posthog-events.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=combine-intercom-with-posthog-events&via=agent

## Problem: Serial Intercom and PostHog calls slow React pages

Each extra Intercom and PostHog hop adds hundreds of milliseconds and another OAuth refresh path your UI must manage.

- One slow upstream blocking an entire page
- Partial failures leaving UI in inconsistent states
- Duplicate auth refresh logic per service

## Solution steps

1. **Add vault secrets for this pipeline**: Add INTERCOM_ACCESS_TOKEN, POSTHOG_PERSONAL_API_KEY to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Configure upstream steps and output mapping**: Use GET against https://api.intercom.io/contacts. 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 React**: Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.

```
const res = await fetch('https://realcomposer.com/v1/workspace/combine-intercom-with-posthog-events', {
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
const data = await res.json();
```

## 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": "Combine intercom with posthog user events",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "intercom",
      "upstreamUrlTemplate": "https://api.intercom.io/contacts",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.INTERCOM_ACCESS_TOKEN}}",
        "Accept": "application/json"
      },
      "timeoutMs": 3000
    },
    {
      "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": "intercom.contactId",
        "sourcePath": "data.0.id",
        "type": "string",
        "sourceStep": "intercom"
      },
      {
        "targetPath": "intercom.email",
        "sourcePath": "data.0.email",
        "type": "string",
        "sourceStep": "intercom"
      },
      {
        "targetPath": "posthog.flagKey",
        "sourcePath": "results.0.key",
        "type": "string",
        "sourceStep": "posthog"
      },
      {
        "targetPath": "posthog.active",
        "sourcePath": "results.0.active",
        "type": "boolean",
        "sourceStep": "posthog"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/combine-intercom-with-posthog-events" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

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

Slimmed RealComposer response:

```json
{
  "intercom_contactId": "sample",
  "intercom_email": "sample",
  "posthog_flagKey": "sample",
  "posthog_active": "sample"
}
```

## FAQ

### What should React store instead of Intercom secrets?

Use scoped tokens where the vendor allows them on combine-intercom-with-posthog-events. Admin keys stay {{secrets.*}} references only.

### How do I forward JSON bodies to POST upstreams?

Cache key for combine-intercom-with-posthog-events: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.

### Can I restrict which domains call this pipeline?

Rotate vault secrets for combine-intercom-with-posthog-events anytime; clients keep the same rc_live_ token.
