> ## Documentation Index
> Fetch the complete documentation index at: https://docs.s4kit.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloud Foundry Deployment

> Deploy S4Kit to SAP BTP Cloud Foundry

Deploy S4Kit to SAP BTP Cloud Foundry for scalable, production-ready hosting.

## Prerequisites

* [Cloud Foundry CLI](https://docs.cloudfoundry.org/cf-cli/install-go-cli.html) installed
* Logged in: `cf login`
* Target set: `cf target -o <org> -s <space>`

## Architecture

| Application      | Purpose           | Port |
| ---------------- | ----------------- | ---- |
| `s4kit-backend`  | Admin API + Auth  | 8080 |
| `s4kit-proxy`    | SDK proxy service | 8080 |
| `s4kit-frontend` | Next.js dashboard | 8080 |

All apps use the [Bun buildpack](https://github.com/michal-majer/cloudfoundry-buildpack-bun).

## Deployment

<Steps>
  <Step title="Create Services">
    ```bash theme={null}
    # PostgreSQL & Redis (external or SAP BTP managed)
    cf create-user-provided-service s4kit-postgres -p '{"uri":"postgresql://..."}'
    cf create-user-provided-service s4kit-redis -p '{"uri":"redis://..."}'

    # Secrets
    cf create-user-provided-service s4kit-secrets -p "{
      \"ENCRYPTION_KEY\": \"$(openssl rand -hex 32)\",
      \"BETTER_AUTH_SECRET\": \"$(openssl rand -base64 32)\"
    }"
    ```
  </Step>

  <Step title="Deploy Apps">
    ```bash theme={null}
    # From monorepo root
    cf push -f packages/platform/backend/manifest.yml
    cf push -f packages/platform/proxy/manifest.yml

    # Frontend (requires build)
    cd packages/platform/frontend
    NEXT_PUBLIC_API_URL=https://s4kit-backend.YOUR-DOMAIN ./deploy-cf.sh
    ```
  </Step>

  <Step title="Verify">
    ```bash theme={null}
    cf apps
    curl https://s4kit-backend.YOUR-DOMAIN/health
    curl https://s4kit-proxy.YOUR-DOMAIN/health/live
    ```
  </Step>
</Steps>

## Configuration

| Variable        | Default    | Description                         |
| --------------- | ---------- | ----------------------------------- |
| `MODE`          | `selfhost` | `selfhost` or `saas` (multi-tenant) |
| `FRONTEND_URL`  | manifest   | Frontend URL for CORS               |
| `COOKIE_DOMAIN` | auto       | Cross-subdomain cookies             |

## Scaling

```bash theme={null}
cf scale s4kit-proxy -i 3 -m 512M    # More API throughput
cf scale s4kit-backend -i 2 -m 256M  # More admin users
```

## XSUAA (SAP Identity)

```bash theme={null}
cd packages/platform/backend
cf create-service xsuaa application s4kit-xsuaa -c xs-security.json
```

Role templates: `S4KitOwner`, `S4KitAdmin`, `S4KitDeveloper`

## Troubleshooting

| Issue         | Solution                                            |
| ------------- | --------------------------------------------------- |
| Module errors | `cf restage s4kit-backend --no-cache`               |
| Redis refused | Check `cf env s4kit-backend \| grep redis`          |
| Cookie issues | Set `COOKIE_DOMAIN` manually                        |
| Wrong API URL | Rebuild frontend with correct `NEXT_PUBLIC_API_URL` |

<Card title="Full CF Guide" icon="github" href="https://github.com/michal-majer/s4kit/blob/main/CF.md">
  Complete Cloud Foundry deployment guide →
</Card>
