# Connect

Connect a Chargebee site with its name and a Full-Access API key, from the dashboard or with the connect API.

## Create an API key

1. Your site name is the part of your Chargebee URL before `.chargebee.com`: `acme` for `acme.chargebee.com`. A test site ends in `-test`.
2. In Chargebee, open **Settings > Configure Chargebee > API Keys** and create a **Full-Access** key. Zernio needs it to add its webhook endpoint.
3. Copy the key.

## Connect from the dashboard

1. Sign in to [Zernio](https://zernio.com/dashboard/connections) and open **Connections**.
2. Click **Connect an account**, pick the profile, and choose **Chargebee** under **Analytics**.
3. Enter the site name and the API key, and click **Connect**. Sales from then on count.

## Connect with the API

```bash
curl -X POST "https://zernio.com/api/v1/connect/chargebee/credentials" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "profileId": "PROFILE_ID", "site": "acme-test", "apiKey": "test_cdHG5wRtYk3eA9pXm2LqZs7vN4bJu8Fw" }'
```

```json
{ "accountId": "6507a1b2c3d4e5f6a7b8c9d1", "platformUserId": "acme-test", "accountName": "acme-test.chargebee.com" }
```

Zernio checks the key, then adds a webhook endpoint (**Settings > Configure Chargebee > Webhooks**) for `payment_succeeded`, `payment_refunded` and `credit_note_created`, pointed at Zernio.

## Disconnect

On **Connections**, click **Disconnect** on the Chargebee account, or call `DELETE /v1/accounts/{accountId}`. Zernio removes the webhook endpoint it added. Sales already stored stay in `GET /v1/events`; sales made while disconnected are not backfilled.

## How it behaves

- A profile holds one Chargebee site. Connecting the same profile again replaces the connection and its webhook endpoint; a new key starts with forwarding off. When Chargebee refuses the new endpoint, nothing changes: the earlier key and endpoint keep working.
- Chargebee signs no webhook, so the endpoint is protected with basic auth: Zernio generates a password for it and checks it on every delivery. Zernio keeps the key and that password encrypted.
- A test site's sales count like live ones; use a separate profile for testing.

## If it fails

| Error | Cause | Fix |
|---|---|---|
| `401 invalid_credentials` | Chargebee refused the site name or the key, or the key is read-only | Check the site name and create a Full-Access key. |
| `400` on `site` | The site is not a bare name | Send only the part before `.chargebee.com`. |
| `502 platform_api_error` | Chargebee did not answer, or did not accept the webhook endpoint (a site holds at most five) | Remove an unused endpoint, then try again. |

## Related

- [Connect a Chargebee site](/connect/connect-chargebee-credentials)
- [Sales](/platforms/chargebee/sales)