# Create a rule (/docs/api-reference/risk/create-risk-rule)

Rules decide after the allow and block lists and before the score thresholds: the first enabled rule (lowest `position`, then oldest) whose conditions match the event sets the decision to its `action`. Conditions test the fields listed in the docs; a field without a value (no IP, no email) never matches. At most 50 rules per environment.

`POST https://api.coss.com/v1/risk/rules`

Authenticate with your API key as a bearer token (`Authorization: Bearer coss_test_…`). Responses are JSON with snake_case fields; errors are `{ error: { type, code, message, param?, issues?, request_id } }` (https://coss.com/docs/api-reference/errors.md).

## Request body

- `name` (string, required, 1-100 characters): Your name for the rule, returned with reasons when it decides.
- `enabled` (boolean): Default `true`.
- `position` (integer, 0-10000): Default 0.
- `events` (("signup" | "login" | "message" | "checkout" | "custom")[], at most 5 items): Events it applies to. Default: every event.
- `match` ("all" | "any"): Default `all`.
- `conditions` (object[], required, 1-20 items): What must hold, by `match`.
  - `field` (string, required, one of "email.valid", "email.disposable", "email.undeliverable", "email.role_account", "email.free_provider", "email.typo", "email.domain_new", "email.domain", "email.domain_age_days", "ip.tor", "ip.hosting", "ip.vpn", "ip.proxy", "ip.relay", "ip.private", "ip.country", "ip.asn", "ip.hosting_provider", "captcha.passed", "duplicate.canonical_mailbox", "duplicate.device", "history.fraud", "login.new_country", "login.new_network", "content.spam", "content.phishing", "content.malware", "content.fraud", "checkout.amount", "checkout.currency", "checkout.card_country", "checkout.card_funding", "checkout.billing_country", "checkout.shipping_country", "device.automation", "device.timezone", "device.language", "score", "velocity.ip_1h", "velocity.ip_24h", "velocity.subnet_1h", "velocity.subnet_24h", "velocity.email_domain_1h", "velocity.email_domain_24h", "velocity.mailbox_1h", "velocity.mailbox_24h", "velocity.user_1h", "velocity.user_24h", "velocity.card_1h", "velocity.card_24h", "velocity.device_1h", "velocity.device_24h"): The signal to test, such as `email.disposable` or `ip.country`.
  - `operator` ("is" | "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "not_in", required): How to compare. Booleans use `is`, numbers `eq` to `lte`, and strings `eq`, `neq`, `in` and `not_in`.
  - `value` (boolean | number | string | string[], required, at most 320 characters, 1-100 items): What to compare it with: its type follows the field, and `in` and `not_in` take a list of strings.
- `action` ("allow" | "review" | "block", required): `allow`: go ahead. `review`: suspicious; let it through with limits, or hold it for a person. `block`: refuse it.

## Example request

TypeScript (the `coss` package):

```ts
import { Coss } from "coss";

const coss = new Coss({
  apiKey: process.env.COSS_API_KEY,
  baseUrl: "https://api.coss.com",
});

const { data, error } = await coss.risk.rules.create({
  name: "Disposable email from a data center",
  events: [
    "signup",
  ],
  conditions: [
    {
      field: "email.disposable",
      operator: "is",
      value: true,
    },
    {
      field: "ip.hosting",
      operator: "is",
      value: true,
    },
  ],
  action: "block",
});
```

HTTP:

```sh
curl -X POST "https://api.coss.com/v1/risk/rules" \
  -H "Authorization: Bearer $COSS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Disposable email from a data center",
  "events": [
    "signup"
  ],
  "conditions": [
    {
      "field": "email.disposable",
      "operator": "is",
      "value": true
    },
    {
      "field": "ip.hosting",
      "operator": "is",
      "value": true
    }
  ],
  "action": "block"
}'
```

Python:

```python
from coss import Coss
from coss import Coss, RiskRuleCondition

client = Coss()  # api_key= or COSS_API_KEY

client.risk.rules.create(
    name="Disposable email from a data center",
    events=[
        "signup"
    ],
    conditions=[
        RiskRuleCondition(
            field="email.disposable",
            operator="is",
            value=True,
        ),
        RiskRuleCondition(
            field="ip.hosting",
            operator="is",
            value=True,
        )
    ],
    action="block",
)
```

Go:

```go
import (
	"context"

	coss "github.com/cosscom/coss-go"
	cossclient "github.com/cosscom/coss-go/client"
	risk "github.com/cosscom/coss-go/risk"
)

client := cossclient.NewClient() // option.WithAPIKey(…) or COSS_API_KEY

request := &risk.CreateRiskRuleRequest{
    Name: "Disposable email from a data center",
    Events: []coss.RiskEvent{
        coss.RiskEventSignup,
    },
    Conditions: []*coss.RiskRuleCondition{
        &coss.RiskRuleCondition{
            Field: coss.RiskRuleConditionFieldEmailDisposable,
            Operator: coss.RiskRuleConditionOperatorIs,
            Value: &coss.RiskRuleConditionValue{
                Boolean: true,
            },
        },
        &coss.RiskRuleCondition{
            Field: coss.RiskRuleConditionFieldIPHosting,
            Operator: coss.RiskRuleConditionOperatorIs,
            Value: &coss.RiskRuleConditionValue{
                Boolean: true,
            },
        },
    },
    Action: coss.RiskDecisionBlock,
}
client.Risk.Rules.Create(
    context.TODO(),
    request,
)
```

Ruby:

```ruby
require "coss"

client = Coss::Client.new # api_key: or COSS_API_KEY

client.risk.rules.create(
  name: "Disposable email from a data center",
  events: ["signup"],
  conditions: [{
    field: "email.disposable",
    operator: "is",
    value: true
  }, {
    field: "ip.hosting",
    operator: "is",
    value: true
  }],
  action: "block"
)
```

PHP:

```php
<?php

use Coss\Coss;
use Coss\Risk\Rules\Requests\CreateRiskRuleRequest;
use Coss\Types\RiskDecision;
use Coss\Types\RiskEvent;
use Coss\Types\RiskRuleCondition;
use Coss\Types\RiskRuleConditionField;
use Coss\Types\RiskRuleConditionOperator;

$client = new Coss(); // apiKey: or COSS_API_KEY

$client->risk->rules->create(
    new CreateRiskRuleRequest([
        'name' => 'Disposable email from a data center',
        'events' => [
            RiskEvent::Signup->value,
        ],
        'conditions' => [
            new RiskRuleCondition([
                'field' => RiskRuleConditionField::EmailDisposable->value,
                'operator' => RiskRuleConditionOperator::Is->value,
                'value' => true,
            ]),
            new RiskRuleCondition([
                'field' => RiskRuleConditionField::IpHosting->value,
                'operator' => RiskRuleConditionOperator::Is->value,
                'value' => true,
            ]),
        ],
        'action' => RiskDecision::Block->value,
    ]),
);
```

Java:

```java
import com.coss.Coss;
import com.coss.resources.risk.rules.requests.CreateRiskRuleRequest;
import com.coss.types.RiskDecision;
import com.coss.types.RiskEvent;
import com.coss.types.RiskRuleCondition;
import com.coss.types.RiskRuleConditionField;
import com.coss.types.RiskRuleConditionOperator;
import com.coss.types.RiskRuleConditionValue;
import java.util.Arrays;
import java.util.Optional;

Coss client = Coss.builder().build(); // .apiKey(…) or COSS_API_KEY

client.risk().rules().create(
    CreateRiskRuleRequest
        .builder()
        .name("Disposable email from a data center")
        .action(RiskDecision.BLOCK)
        .conditions(
            Arrays.asList(
                RiskRuleCondition
                    .builder()
                    .field(RiskRuleConditionField.EMAIL_DISPOSABLE)
                    .operator(RiskRuleConditionOperator.IS)
                    .value(
                        RiskRuleConditionValue.of(true)
                    )
                    .build(),
                RiskRuleCondition
                    .builder()
                    .field(RiskRuleConditionField.IP_HOSTING)
                    .operator(RiskRuleConditionOperator.IS)
                    .value(
                        RiskRuleConditionValue.of(true)
                    )
                    .build()
            )
        )
        .events(
            Optional.of(
                Arrays.asList(RiskEvent.SIGNUP)
            )
        )
        .build()
);
```

.NET:

```csharp
using Coss;
using Coss.Risk;

var client = new CossClient(); // apiKey: or COSS_API_KEY

await client.Risk.Rules.CreateAsync(
    new CreateRiskRuleRequest
    {
        Name = "Disposable email from a data center",
        Events = new List<RiskEvent>() { RiskEvent.Signup },
        Conditions = new List<RiskRuleCondition>()
        {
            new RiskRuleCondition
            {
                Field = RiskRuleConditionField.EmailDisposable,
                Operator = RiskRuleConditionOperator.Is,
                Value = true,
            },
            new RiskRuleCondition
            {
                Field = RiskRuleConditionField.IpHosting,
                Operator = RiskRuleConditionOperator.Is,
                Value = true,
            },
        },
        Action = RiskDecision.Block,
    }
);
```

## Response 201

The new rule.

- `object` ("risk_rule", required)
- `id` (string, required)
- `name` (string, required): Your name for the rule, returned with reasons when it decides.
- `enabled` (boolean, required): `false`: it's kept, and no assessment applies it.
- `position` (integer, required): Rules are tried in ascending order.
- `events` (("signup" | "login" | "message" | "checkout" | "custom")[], required): Events it applies to; empty: every event.
- `match` ("all" | "any", required): `all`: every condition must hold. `any`: one is enough.
- `conditions` (object[], required): What must hold, by `match`.
  - `field` (string, required, one of "email.valid", "email.disposable", "email.undeliverable", "email.role_account", "email.free_provider", "email.typo", "email.domain_new", "email.domain", "email.domain_age_days", "ip.tor", "ip.hosting", "ip.vpn", "ip.proxy", "ip.relay", "ip.private", "ip.country", "ip.asn", "ip.hosting_provider", "captcha.passed", "duplicate.canonical_mailbox", "duplicate.device", "history.fraud", "login.new_country", "login.new_network", "content.spam", "content.phishing", "content.malware", "content.fraud", "checkout.amount", "checkout.currency", "checkout.card_country", "checkout.card_funding", "checkout.billing_country", "checkout.shipping_country", "device.automation", "device.timezone", "device.language", "score", "velocity.ip_1h", "velocity.ip_24h", "velocity.subnet_1h", "velocity.subnet_24h", "velocity.email_domain_1h", "velocity.email_domain_24h", "velocity.mailbox_1h", "velocity.mailbox_24h", "velocity.user_1h", "velocity.user_24h", "velocity.card_1h", "velocity.card_24h", "velocity.device_1h", "velocity.device_24h"): The signal to test, such as `email.disposable` or `ip.country`.
  - `operator` ("is" | "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "not_in", required): How to compare. Booleans use `is`, numbers `eq` to `lte`, and strings `eq`, `neq`, `in` and `not_in`.
  - `value` (boolean | number | string | string[], required, at most 320 characters, 1-100 items): What to compare it with: its type follows the field, and `in` and `not_in` take a list of strings.
- `action` ("allow" | "review" | "block", required): `allow`: go ahead. `review`: suspicious; let it through with limits, or hold it for a person. `block`: refuse it.
- `created_at` (string, required, date-time)
- `updated_at` (string, required, date-time)

Example:

```json
{
  "object": "risk_rule",
  "id": "rskrule_01k7c8m4q2f3e9v6t5r1n0p8yz",
  "name": "Disposable email from a data center",
  "enabled": true,
  "position": 0,
  "events": [
    "signup"
  ],
  "match": "all",
  "conditions": [
    {
      "field": "email.disposable",
      "operator": "is",
      "value": true
    },
    {
      "field": "ip.hosting",
      "operator": "is",
      "value": true
    }
  ],
  "action": "block",
  "created_at": "2026-01-15T09:30:00.000Z",
  "updated_at": "2026-01-15T09:30:00.000Z"
}
```

## Errors

- `400`: The body is not valid JSON (`invalid_json`). Also when the `Coss-Version` header names an unknown API version (`invalid_api_version`).
- `401`: The API key is missing, invalid, revoked or expired (`authentication_error`).
- `403`: The API key lacks the permission this endpoint needs (`permission_error`), or its organization is suspended (`organization_suspended`).
- `413`: The request body is larger than 5 MB (`payload_too_large`).
- `415`: The body is not sent as `application/json` (`unsupported_media_type`).
- `422`: The body failed validation (`validation_error`, see `issues`) or the environment already has 50 rules (`too_many_rules`).
- `429`: Too many requests with this API key (`rate_limited`), or too many requests with invalid keys from this address (`too_many_failed_authentications`). Retry after `Retry-After` seconds. Successful responses carry `RateLimit-Limit`, `RateLimit-Remaining` and `RateLimit-Reset` (seconds until the limit is fully restored).
- `500`: Unexpected server error (`api_error` / `internal_error`).
- `503`: The API key could not be verified right now; retry (`service_unavailable`).