# Customer portal

A page we run where your customer changes plan, seats and cards and downloads invoices, without you building any of it.

## Open a session

Needs a secret key with the `portal:write` scope. The customer must already exist.

**Node**

```ts title="portal.ts"
const portal = await mint.portal.create({
  customerExternalId: 'ws_123',
  returnUrl: 'https://app.example.com/settings',
})

redirect(portal.url)
```

**curl**

```bash title="terminal"
curl -X POST https://globalymint.com/api/v1/portal-sessions \
  -H "Authorization: Bearer $MINT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customer_external_id": "ws_123",
    "return_url": "https://app.example.com/settings"
  }'
```

The reply has the session `id`, its `url`, `expires_at` and your `return_url`. A portal session is for one customer and is valid for one hour. Open a new one each time the customer clicks your Manage billing button.

## What the customer can do

- Change plan. An upgrade applies now and is prorated; a downgrade applies at the end of the period.
- Change seats, with the prorated amount shown before they confirm.
- Cancel at the end of the period, and undo that.
- Add, remove and choose a default payment method.
- Edit billing details and tax ID.
- Download invoice PDFs.
- Buy credit packs, where you sell them.

Every action is checked against the session's customer. A subscription, payment method or invoice that belongs to someone else answers 404.

## Limit what the portal offers

Plan and seat changes can be switched off per product:

```ts title="product.ts"
await mint.catalog.updateProduct(productId, {
  portalAllowPlanChange: false,
  portalAllowSeatChange: false,
})
```

The portal then hides those actions, and refuses them with `422 change_not_allowed` if they are attempted anyway.

## Keep your app in step

The portal changes subscriptions directly, so listen for the webhooks: `subscription.updated`, `subscription.canceled` and `entitlements.updated`. Your app should read what a customer may use from their entitlements, not from what your own UI last did.
