# Make a keyword rule

> When an incoming one-to-one text matches, the rule replies with words or a template, tags the person, adds them to a sequence, or sends an automation.matched webhook event, in the order you list them.

`POST /v1/automations/rules`

When an incoming one-to-one text matches, the rule replies with words or a template, tags the person, adds them to a sequence, or sends an `automation.matched` webhook event, in the order you list them. Only the first matching rule runs.

`match` is `exact` (the whole text is the keyword, ignoring case and punctuation at the ends), `starts_with`, `word` (the keyword as a whole word anywhere), `contains`, or `regex`. A stop word never matches: it stops the person and nothing else.

Needs a key with the `messages:send` scope. [Keys and scopes](https://usebluebot.com/docs/concepts/api-keys)

Send an `Idempotency-Key` so a retry never acts twice. [Idempotency](https://usebluebot.com/docs/concepts/idempotency)

## Headers

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string | No | Any string of up to 255 characters, new for each request. A retry with the same key gets the first answer back for 24 hours, with `Idempotent-Replayed: true`. Up to 255 characters |

## Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | Yes | Up to 80 characters |
| `match` | string | Yes | One of `exact`, `starts_with`, `word`, `contains`, `regex` |
| `keywords` | array of strings | No | Up to 20 items |
| `pattern` | string | No | Up to 500 characters |
| `case_sensitive` | boolean | No |   |
| `actions` | array of any | Yes | Up to 5 items |
| `enabled` | boolean | No |   |
| `position` | integer | No | 0 to 10,000 |

## Example

```bash
curl -X POST "https://usebluebot.com/api/v1/automations/rules" \
  -H "Authorization: Bearer $BLUEBOT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 0f6d8c2e-5b1a-4c7e-9d3f-2a8b6e1c4d70" \
  -d '{
    "name": "Pricing",
    "match": "word",
    "keywords": [
      "price",
      "pricing",
      "cost"
    ],
    "actions": [
      {
        "type": "reply",
        "text": "Our plans start at $29 a month. Want me to send the details?"
      }
    ]
  }'
```

```ts
import BlueBot from '@usebluebot/sdk';

const client = new BlueBot({ apiKey: process.env.BLUEBOT_API_KEY });

const rule = await client.automations.rules.create({
  name: 'Pricing',
  match: 'word',
  keywords: ['price', 'pricing', 'cost'],
  actions: [
    {
      type: 'reply',
      text: 'Our plans start at $29 a month. Want me to send the details?',
    },
  ],
}, { idempotencyKey: '0f6d8c2e-5b1a-4c7e-9d3f-2a8b6e1c4d70' });
console.log(rule);
```

```python
import os
from bluebot import BlueBot

client = BlueBot(api_key=os.environ["BLUEBOT_API_KEY"])

rule = client.automations.rules.create(
    name="Pricing",
    match="word",
    keywords=["price", "pricing", "cost"],
    actions=[
        {
            "type": "reply",
            "text": "Our plans start at $29 a month. Want me to send the details?",
        },
    ],
    idempotency_key="0f6d8c2e-5b1a-4c7e-9d3f-2a8b6e1c4d70",
)
print(rule)
```

## Response

`201`: The rule.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | Yes | A kw_ id. Starts with `kw_` |
| `object` | "keyword_rule" | Yes |   |
| `name` | string | Yes |   |
| `match` | string | Yes | `exact` (the whole text), `starts_with`, `word` (a whole word anywhere), `contains`, or `regex`. One of `exact`, `starts_with`, `word`, `contains`, `regex` |
| `keywords` | array of strings | Yes |   |
| `pattern` | string or null | Yes | For `regex`. |
| `case_sensitive` | boolean | Yes |   |
| `actions` | array of any | Yes |   |
| `position` | integer | Yes | Rules are tried from the lowest position up; the first match wins. |
| `enabled` | boolean | Yes |   |
| `fire_count` | integer | Yes | How many texts it answered. |
| `last_fired_at` | timestamp or null | Yes | When it last matched. |
| `created_at` | timestamp | Yes | When it was made. |

The answer, `201`:

```json
{
  "id": "kw_1Hq2LxW9cVbN3mKpRtYs0a",
  "object": "keyword_rule",
  "name": "Pricing",
  "match": "word",
  "keywords": [
    "price",
    "pricing",
    "cost"
  ],
  "pattern": null,
  "case_sensitive": false,
  "actions": [
    {
      "type": "reply",
      "text": "Our plans start at $29 a month. Want me to send the details?",
      "template_id": null
    },
    {
      "type": "tag",
      "tags": [
        "asked-pricing"
      ]
    }
  ],
  "position": 10,
  "enabled": true,
  "fire_count": 0,
  "last_fired_at": null,
  "created_at": "2026-11-02T15:00:00.000Z"
}
```

## Errors

Every refusal has the same envelope and a page of its own. These are the ones this operation can give.

| Code                                                        | Status | What it means                                                                                                                                    |
| ----------------------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| [`unauthorized`](https://usebluebot.com/docs/errors/unauthorized)                 | 401    | The request carried no key, or a key BlueBot doesn’t know: mistyped, revoked, or a test key sent to the live API.                                |
| [`insufficient_scope`](https://usebluebot.com/docs/errors/insufficient_scope)     | 403    | The key doesn’t carry the scope the endpoint needs. Each endpoint’s reference names its scope.                                                   |
| [`rate_limited`](https://usebluebot.com/docs/errors/rate_limited)                 | 429    | Too many requests. A key may make 120 requests a minute. The `RateLimit` header says how many are left, and `Retry-After` how long to wait.      |
| [`internal`](https://usebluebot.com/docs/errors/internal)                         | 500    | Something went wrong on BlueBot’s side. It was reported. Send the request again; with an Idempotency-Key that is always safe.                    |
| [`invalid_request`](https://usebluebot.com/docs/errors/invalid_request)           | 400    | Something in the request doesn’t fit: a missing or malformed field, a bad id, or a combination the endpoint refuses. `details` names each field. |
| [`not_found`](https://usebluebot.com/docs/errors/not_found)                       | 404    | There is no such thing on this account. Ids are prefixed by kind, so a message id starts with `msg_`.                                            |
| [`limit_reached`](https://usebluebot.com/docs/errors/limit_reached)               | 409    | The account has as many of something as it may have.                                                                                             |
| [`plan_lapsed`](https://usebluebot.com/docs/errors/plan_lapsed)                   | 402    | The account’s plan isn’t active, so BlueBot isn’t taking new texts. Texts already queued wait and go once the plan is active again.              |
| [`idempotency_conflict`](https://usebluebot.com/docs/errors/idempotency_conflict) | 409    | The Idempotency-Key was used in the last 24 hours with a different request. A key stands for one request.                                        |

## Try it
