# Quickstart

Create an API key, make your first request and receive your first webhook.

You need a Chatwise workspace you are an admin of, and Node.js 20 or later, or Python 3.9 or later, to run the samples.

## 1. Create an API key

1. In the dashboard, open **Settings → Developer → API Keys** ([open it](https://app.chatwise.so/settings/api-keys)).
2. Select **New key** and name it.
3. Choose **Read conversations**, **Read messages**, **Read webhook endpoints** and **Manage webhook endpoints**.
4. Create it and copy the key. It is shown once.

Keep the key on your server, for example in an environment variable:

```bash
export CHATWISE_API_KEY='paste your key here'
```

## 2. Make a request

List the latest conversations:

**curl**:

```bash
curl 'https://api.chatwise.so/v1/conversations?limit=5' \
  -H "Authorization: Bearer $CHATWISE_API_KEY"
```

**Node.js**:

```js
const response = await fetch("https://api.chatwise.so/v1/conversations?limit=5", {
  headers: { Authorization: `Bearer ${process.env.CHATWISE_API_KEY}` },
});
const body = await response.json();
if (!response.ok) throw new Error(`${response.status}: ${body.message}`);
console.log(body.data);
```

**Python**:

```python
import json
import os
import urllib.request

request = urllib.request.Request(
    "https://api.chatwise.so/v1/conversations?limit=5",
    headers={"Authorization": f"Bearer {os.environ['CHATWISE_API_KEY']}"},
)
with urllib.request.urlopen(request) as response:
    print(json.load(response)["data"])
```

`data` holds the conversations, and `pagination.nextCursor` leads to the [next page](https://chatwise.so/docs/pagination.md). An answer that isn't 2xx has a `message` that says what is wrong: see [errors](https://chatwise.so/docs/errors.md).

## 3. Receive a webhook

Chatwise sends events to a public `https://` URL on port 443. While you develop, a tunnel to your machine gives you one.

**Create the endpoint.** Replace the URL with yours:

```bash
curl -X POST 'https://api.chatwise.so/v1/webhook-endpoints' \
  -H "Authorization: Bearer $CHATWISE_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Quickstart",
  "url": "https://example.com/webhooks/chatwise",
  "events": ["message.received"]
}'
```

The answer has the endpoint's `id` and its `signingSecret`. The secret is shown only now, so keep it like the API key:

```bash
export CHATWISE_WEBHOOK_SECRET='paste the signing secret here'
```

**Run a receiver.** Copy the server and the function it uses from [verify signatures](https://chatwise.so/docs/webhooks/verify-signatures.md) into two files, and start it where your URL leads:

**Node.js**:

```bash
node server.mjs
```

**Python**:

```bash
python3 server.py
```

**Send a test event.** Use the endpoint's `id`:

```bash
curl -X POST 'https://api.chatwise.so/v1/webhook-endpoints/we_01JE2Z3K8Q9V7W5X4Y3Z2A1B0C/test' \
  -H "Authorization: Bearer $CHATWISE_API_KEY"
```

The answer says what your endpoint did:

```json
{
  "data": {
    "delivered": true,
    "statusCode": 204,
    "durationMs": 182,
    "error": null
  }
}
```

If `delivered` is false, see [testing](https://chatwise.so/docs/webhooks/testing.md).

**Write to your own inbox.** Send a message to one of your connected accounts from another phone or account. Your receiver gets a [`message.received`](https://chatwise.so/docs/webhooks/events/message-received.md) event.

## Next

- [Delivery and retries](https://chatwise.so/docs/webhooks/delivery.md): what Chatwise does with each answer, and why to dedupe.
- [Authentication and scopes](https://chatwise.so/docs/authentication.md): give each integration only what it needs.
- [API reference](https://chatwise.so/docs/api.md): every route.
