# Merge cloudinary assets with contentful entries

> Resolve Contentful asset references with Cloudinary transformation metadata. For "merge cloudinary assets with contentful entries", store CLOUDINARY_BASIC_AUTH, CONTENTFUL_ACCESS_TOKEN in Secret Vault, call https://api.cloudinary.com/v1_1/{{secrets.CLOUDINARY_CLOUD_NAME}}/resources/image from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/merge-cloudinary-with-contentful-entries with an rc_live_ client token.

- Canonical page: https://realcomposer.com/recipes/merge-cloudinary-with-contentful-entries
- Client platform: Next.js / React
- Upstream services: Cloudinary, Contentful
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/merge-cloudinary-with-contentful-entries.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=merge-cloudinary-with-contentful-entries&via=agent

## Problem: Waterfall latency from multiple Cloudinary and Contentful requests

Next.js / React widgets often await Cloudinary and Contentful 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**: Create CLOUDINARY_BASIC_AUTH, CONTENTFUL_ACCESS_TOKEN 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://api.cloudinary.com/v1_1/{{secrets.CLOUDINARY_CLOUD_NAME}}/resources/image. 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 Next.js / React**: Allowed origins on the token should list every Next.js / React host that calls this URL.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/merge-cloudinary-with-contentful-entries', {
  cache: 'no-store',
  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 cloudinary assets with contentful entries",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "cloudinary",
      "upstreamUrlTemplate": "https://api.cloudinary.com/v1_1/{{secrets.CLOUDINARY_CLOUD_NAME}}/resources/image",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Basic {{secrets.CLOUDINARY_BASIC_AUTH}}"
      },
      "timeoutMs": 3000
    },
    {
      "stepKey": "contentful",
      "upstreamUrlTemplate": "https://cdn.contentful.com/spaces/{{query.spaceId}}/entries",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.CONTENTFUL_ACCESS_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "cloudinary.publicId",
        "sourcePath": "resources.0.public_id",
        "type": "string",
        "sourceStep": "cloudinary"
      },
      {
        "targetPath": "cloudinary.format",
        "sourcePath": "resources.0.format",
        "type": "string",
        "sourceStep": "cloudinary"
      },
      {
        "targetPath": "contentful.entryId",
        "sourcePath": "items.0.sys.id",
        "type": "string",
        "sourceStep": "contentful"
      },
      {
        "targetPath": "contentful.title",
        "sourcePath": "items.0.fields.title",
        "type": "string",
        "sourceStep": "contentful"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/merge-cloudinary-with-contentful-entries" \
  -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-cloudinary-with-contentful-entries"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "cloudinary_publicId": "sample",
  "cloudinary_format": "sample",
  "contentful_entryId": "sample",
  "contentful_title": "sample"
}
```

## FAQ

### Does Next.js / React need CORS changes after adding RealComposer?

Use scoped tokens where the vendor allows them on merge-cloudinary-with-contentful-entries. Admin keys stay {{secrets.*}} references only.

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

merge-cloudinary-with-contentful-entries forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

### How do I rotate upstream credentials?

Upstream steps in merge-cloudinary-with-contentful-entries honor timeoutMs and optional fallback JSON without leaking secrets.
