# Quickstart

Create a customer, send them to checkout and receive the webhook. In test mode, from your server.

Samples use a placeholder key. Create a test key in the dashboard under Developers › API keys.

## 1. Install and authenticate

A secret key starts with `gpy_test_` or `gpy_live_`. Send it as `Authorization: Bearer …` from your server only. The key's prefix decides the mode, so a test key can never touch live data.

Create a test key in the dashboard under Developers › API keys. It is shown once.

> **Not released yet.** Planned for Phase 6 of the release; confirm before relying on it.
>
>
> The SDK is installed from GitHub Packages with `npm install @globaly-inc/mint-sdk`. Until it is published there, install the tarball attached to a GitHub release, or call the REST API directly as in the curl tab.
>

**Node**

```ts title="lib/mint.ts"
import { Mint } from '@globaly-inc/mint-sdk'

// One client per process. The mode comes from the key.
export const mint = new Mint({
  secretKey: process.env.MINT_SECRET_KEY!, // gpy_test_…
})
```

**curl**

```bash title="terminal"
export MINT_SECRET_KEY=gpy_test_…

curl https://globalymint.com/api/v1/customers \
  -H "Authorization: Bearer $MINT_SECRET_KEY"
```

## 2. Create the customer

A customer is keyed by your own id for the account: a workspace or organisation id. Upserting is safe to repeat, so call it on every sign-in.

Checkout does not create customers. A session for an id we have not seen answers 404.

**Node**

```ts title="customer.ts"
await mint.customers.upsert({
  externalId: 'ws_123',
  email: 'owner@acme.dev',
  name: 'Acme Inc',
})
```

**curl**

```bash title="terminal"
curl -X PUT https://globalymint.com/api/v1/customers/by-external-id/ws_123 \
  -H "Authorization: Bearer $MINT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{"email":"owner@acme.dev","name":"Acme Inc"}'
```

## 3. Open a checkout

Name the plan by its code. Create plans in the dashboard under Products, or with `mint.catalog.upsertPlan`. Both URLs are required and must be https (http is accepted for localhost).

Redirect the browser to `session.url`. The link is valid for 24 hours. The page collects the card through Stripe, so connect your Stripe test keys under Settings › Payments first, then pay with Stripe's test card `4242 4242 4242 4242`.

**Node**

```ts title="checkout.ts"
const session = await mint.checkout.create({
  customerExternalId: 'ws_123',
  planCode: 'pro_monthly',
  seats: 5,
  successUrl: 'https://app.acme.dev/billing?ok=1',
  cancelUrl: 'https://app.acme.dev/billing',
})

redirect(session.url)
```

**curl**

```bash title="terminal"
curl -X POST https://globalymint.com/api/v1/checkout-sessions \
  -H "Authorization: Bearer $MINT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customer_external_id": "ws_123",
    "items": [{ "plan_code": "pro_monthly", "quantity": 5 }],
    "success_url": "https://app.acme.dev/billing?ok=1",
    "cancel_url": "https://app.acme.dev/billing"
  }'
```

## 4. Receive the webhook

Add an endpoint under Developers › Webhooks and copy its `whsec_` secret; it is shown once. The URL must be https on a public host, so use a tunnel while you develop locally.

When the payer finishes, we send `checkout.session.completed` and, once the first invoice is paid, `invoice.paid`. Grant access on the webhook, not on the redirect to your success URL. Order is not guaranteed and delivery is at least once, so skip any `event.id` you have already handled.

```ts title="app/api/mint/webhooks/route.ts"
import { mint } from '@/lib/mint'

export const POST = mint.webhooks.handler({
  secret: process.env.MINT_WEBHOOK_SECRET!, // whsec_…
  on: {
    'invoice.paid': async (event) => {
      // event.id is stable: use it to skip duplicates.
      await grantAccess(event.data.customer_external_id)
    },
  },
})
```

## Next

- [Verify signatures](/docs/webhooks/verify-signatures) without the SDK.
- [Gate features on entitlements](/docs/sdk) so your app asks GlobalyMint what a customer may use.
- [Open the customer portal](/docs/portal) from your settings page.
