# cURL Integration — RenderPix

**API Endpoint:** `POST https://renderpix.dev/v1/render`

---

## Authentication

Pass your API key via the `X-API-Key` header.

```bash
X-API-Key: rpx_your_api_key
```

---

## Basic Example — HTML to PNG

```bash
curl -X POST https://renderpix.dev/v1/render \
  -H "X-API-Key: rpx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"html":"<h1 style=\"font-family:sans-serif\">Hello, RenderPix!</h1>","width":1200,"height":630,"format":"png"}' \
  -o output.png
```

---

## Basic Example — Save as WebP

```bash
curl -X POST https://renderpix.dev/v1/render \
  -H "X-API-Key: rpx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"html":"<h1>Hello</h1>","width":1200,"height":630,"format":"webp"}' \
  -o output.webp
```

---

## Real-World Example — OG Image Generator

Generate a 1200×630 social card and save it as `og.png`:

```bash
curl -X POST https://renderpix.dev/v1/render \
  -H "X-API-Key: rpx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><style>*{margin:0;padding:0;box-sizing:border-box}body{width:1200px;height:630px;overflow:hidden;font-family:-apple-system,BlinkMacSystemFont,sans-serif;background:linear-gradient(135deg,#0f172a 0%,#1e293b 100%);display:flex;align-items:center;justify-content:center;padding:80px}.card{display:flex;flex-direction:column;gap:24px;width:100%}.domain{font-size:20px;font-weight:600;color:#38bdf8;text-transform:uppercase;letter-spacing:2px}h1{font-size:72px;font-weight:800;color:#f1f5f9;line-height:1.1}p{font-size:28px;color:#94a3b8;line-height:1.5}</style></head><body><div class=\"card\"><div class=\"domain\">renderpix.dev</div><h1>Build Faster with RenderPix</h1><p>HTML to image in one API call.</p></div></body></html>",
    "width": 1200,
    "height": 630,
    "format": "png"
  }' \
  -o og.png

echo "OG image saved: og.png"
```

---

## Using a Variable for HTML

For longer HTML, use a shell variable or read from a file:

```bash
API_KEY="rpx_your_api_key"

HTML='<html><body style="margin:0;width:800px;height:600px;background:#0f172a;display:flex;align-items:center;justify-content:center"><h1 style="color:#f1f5f9;font-family:sans-serif">Hello!</h1></body></html>'

curl -X POST https://renderpix.dev/v1/render \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"html\": $(echo "$HTML" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))'), \"width\": 800, \"height\": 600}" \
  -o output.png
```

Or use `jq` for clean JSON construction:

```bash
API_KEY="rpx_your_api_key"
HTML=$(cat template.html)

jq -n --arg html "$HTML" \
  '{html: $html, width: 1200, height: 630, format: "png"}' \
| curl -X POST https://renderpix.dev/v1/render \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d @- \
  -o output.png
```

---

## Response Handling

Check the HTTP status code before treating the output as an image:

```bash
STATUS=$(curl -X POST https://renderpix.dev/v1/render \
  -H "X-API-Key: rpx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"html":"<h1>Hello</h1>","width":1200,"height":630}' \
  -o output.png \
  -w "%{http_code}" \
  --silent)

if [ "$STATUS" = "200" ]; then
  echo "Success — image saved to output.png"
else
  echo "Error: HTTP $STATUS"
  cat output.png  # contains JSON error body on failure
fi
```

---

## Error Handling

```bash
#!/bin/bash
set -euo pipefail

API_KEY="rpx_your_api_key"

render() {
  local html="$1"
  local output="${2:-output.png}"
  local tmp_err
  tmp_err=$(mktemp)

  STATUS=$(curl -s -X POST https://renderpix.dev/v1/render \
    -H "X-API-Key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d "{\"html\":$(printf '%s' "$html" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))'),\"width\":1200,\"height\":630}" \
    -o "$output" \
    -w "%{http_code}" 2>"$tmp_err")

  case "$STATUS" in
    200) echo "Saved to $output" ;;
    401) echo "Error: Invalid API key"; exit 1 ;;
    429) echo "Error: Rate limit exceeded"; exit 1 ;;
    422) echo "Error: Invalid request — $(cat "$output")"; exit 1 ;;
    *)   echo "Error: HTTP $STATUS — $(cat "$output")"; exit 1 ;;
  esac

  rm -f "$tmp_err"
}

render '<h1 style="font-family:sans-serif">Hello!</h1>' my-image.png
```

---

## GitHub Actions — Generate OG images on deploy

```yaml
# .github/workflows/og-images.yml
name: Generate OG Images

on:
  push:
    branches: [main]

jobs:
  og-images:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Render OG image
        env:
          RENDERPIX_API_KEY: ${{ secrets.RENDERPIX_API_KEY }}
        run: |
          curl -X POST https://renderpix.dev/v1/render \
            -H "X-API-Key: $RENDERPIX_API_KEY" \
            -H "Content-Type: application/json" \
            -d '{"html":"<html><body style=\"margin:0;width:1200px;height:630px;background:#0f172a;display:flex;align-items:center;justify-content:center\"><h1 style=\"color:#f1f5f9;font-family:sans-serif;font-size:80px\">My Blog</h1></body></html>","width":1200,"height":630}' \
            -o public/og.png
          echo "OG image generated"
```
