# Supabase recipes

> Text someone when a row changes, from a database webhook or an Edge Function.

Three recipes for Supabase. Each names its trigger and the DogBot step, lists the fields to map and says how to test it with your own number before anyone else gets a text.

## How Supabase connects

- **[API](https://getdogbot.com/docs/api)**: Your own code calls DogBot’s REST API and listens to its signed webhooks.
- **[Text Hook](https://getdogbot.com/docs/text-hooks)**: A Text Hook is a DogBot address the app posts its own webhook to. You map its fields to a recipient and a template, and each post becomes a text.

## Text a customer when a booking row is added

| Part             | What it is                                          |
| ---------------- | --------------------------------------------------- |
| Trigger          | A Supabase Database Webhook on inserts into a table |
| Action           | A DogBot **Text Hook** turns each post into a text  |
| Connects through | [Text Hook](https://getdogbot.com/docs/text-hooks)                       |

### Steps

1. Make a Text Hook in DogBot and copy its address.
2. Add a Database Webhook in Supabase that posts inserts on your bookings table to that address.
3. Insert a test row, then map record.phone and record.first_name from the payload DogBot kept.
4. Write the template, and each new row becomes a text.

### Map the fields

| Text Hook setting | Set it to                                                                                                      |
| ----------------- | -------------------------------------------------------------------------------------------------------------- |
| Recipient         | The field in Supabase’s post that holds the person’s phone number, written as a path such as `{{body.phone}}`. |
| Text              | Hi {{body.first_name}}, your booking is confirmed. Reply here if you need to change it.                      |
| Metadata          | `source` set to `supabase`. DogBot adds the hook’s id by itself.                                               |

### Test it

Post a sample to the hook’s address with your own number in the phone field, as below. A text arrives on your phone, and the hook’s page lists the post.

```bash title="A test post shaped like Supabase’s"
curl -X POST "https://getdogbot.com/h/YOUR_HOOK_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"phone":"+15555550123","first_name":"Maria"}'
```

## Send from an Edge Function

| Part             | What it is              |
| ---------------- | ----------------------- |
| Trigger          | Your Edge Function runs |
| Action           | `POST /v1/messages`     |
| Connects through | [API](https://getdogbot.com/docs/api)        |

### Steps

1. Store your API key with supabase secrets set.
2. Import dogbot from npm in the function and send with to and text.
3. Pass an Idempotency-Key made from the row’s ID, so a retried function never sends twice.

### Map the fields

| DogBot field                 | Map it to                                                                                                                                     |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `to`                         | The person’s mobile number from Supabase. US numbers can leave off the +1.                                                                    |
| `text`                       | Hi {{first_name}}, your order is confirmed. We’ll text you here when it’s on its way.                                                       |
| `{{first_name}}` in the text | Supabase’s first name field.                                                                                                                  |
| `metadata`                   | `source` set to `supabase`, and `record_id` set to the record’s id, so the status and reply webhooks for this text can find the record again. |
| `service`                    | Auto, the default: iMessage when the person uses it, otherwise SMS through your iPhone.                                                       |

```bash
curl -X POST "https://getdogbot.com/api/v1/messages" \
  -H "Authorization: Bearer $DOGBOT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: supabase-1042" \
  -d '{
    "to": "+15555550123",
    "text": "Hi Maria, your order is confirmed. We’ll text you here when it’s on its way.",
    "metadata": {
      "source": "supabase",
      "record_id": "1042"
    }
  }'
```

```ts
import DogBot from '@getdogbot/sdk';

const client = new DogBot({ apiKey: process.env.DOGBOT_API_KEY });

const message = await client.messages.send({
  to: '+15555550123',
  text: 'Hi Maria, your order is confirmed. We’ll text you here when it’s on its way.',
  metadata: {
    source: 'supabase',
    record_id: '1042',
  },
}, { idempotencyKey: 'supabase-1042' });
console.log(message);
```

```python
import os
from dogbot import DogBot

client = DogBot(api_key=os.environ["DOGBOT_API_KEY"])

message = client.messages.send(
    to="+15555550123",
    text="Hi Maria, your order is confirmed. We’ll text you here when it’s on its way.",
    metadata={
        "source": "supabase",
        "record_id": "1042",
    },
    idempotency_key="supabase-1042",
)
print(message)
```

### Test it

Run the request with your own number in `to`. A text arrives on your phone from your own number.

## Save replies to a table

| Part             | What it is                                                              |
| ---------------- | ----------------------------------------------------------------------- |
| Trigger          | A text comes in (DogBot’s message.received webhook)                     |
| Action           | A webhook for `message.received` starts it, and Supabase takes the text |
| Connects through | [API](https://getdogbot.com/docs/api)                                                        |

### Steps

1. Deploy an Edge Function and add its URL in DogBot as a webhook for message.received.
2. Check the Standard Webhooks signature with your signing secret before anything else.
3. Insert the sender, the words and the time into a replies table.

### Map the fields

| From DogBot        | Into Supabase                                                                                                                                            |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data.from`        | The phone number to find the person by in Supabase. It’s in E.164 (`+15555550123`); search by the last 10 digits if Supabase stores numbers another way. |
| `data.text`        | The words, for a note, a row or a message. Null when the person sent only a picture.                                                                     |
| `data.received_at` | When it came in, in UTC.                                                                                                                                 |
| `data.files`       | Pictures and voice memos. Each `url` works for an hour, so copy a file you want to keep.                                                                 |
| `data.chat_id`     | Set when the text came in a group chat; null for a one-to-one text.                                                                                      |

### Test it

Run `dogbot listen --forward-to http://localhost:3000/webhooks`, then text your DogBot number from another phone. The CLI forwards the event to your handler, signed. DogBot copies texts from people you’ve texted through it, unless you set **Which chats to copy** to **Everything**.

## Before you turn it on

- Text only people who asked to hear from you. Anyone who replies STOP goes on your stop list, and DogBot cancels texts to them. [Stop words](https://getdogbot.com/docs/concepts/stop-words)
- A text that can’t go yet waits with a hold rather than failing: texting hours, your Mac away, a daily limit. [Holds](https://getdogbot.com/docs/concepts/holds)
- [Supabase with DogBot](https://getdogbot.com/integrations/supabase) on the integrations page says what else the two do together.
