# Create a firewall rule

Add a rule to a firewall. The rule applies to the firewall's servers shortly after the response.

[endpoint]
method = "POST"
base_url = "https://{availability_zone}.betterstackcloud.com"
path = "/v1/firewalls/{firewall_id}/rules"

[[path_param]]
name = "firewall_id"
required = true
type = "string"
description = "The ID of the firewall to add the rule to."

[[body_param]]
name = "direction"
required = true
type = "string"
description = "Which traffic the rule matches.<br/>Valid values:<br/>`inbound`: The rule matches traffic that arrives at the server.<br/>`outbound`: The rule matches traffic that leaves the server."

[[body_param]]
name = "protocol"
required = true
type = "string"
description = "The IP protocol the rule matches. `any` matches every protocol. Possible values: `tcp`, `udp`, `icmp`, `gre`, `esp`, `any`."

[[body_param]]
name = "ip_addresses"
required = true
type = "array of strings"
description = "The addresses the rule applies to, in CIDR notation, such as `10.0.0.0/8`. Use `0.0.0.0/0` and `::/0` for every address. The list can't be empty."

[[body_param]]
name = "port_from"
required = true
type = "integer"
description = "The first port of the range, from `0` to `65535`. Every rule needs it. A protocol without ports, such as `icmp`, ignores it."

[[body_param]]
name = "port_to"
type = "integer or null"
description = "The last port of the range, from `port_from` to `65535`. Leave it out to match only `port_from`."

[[header]]
name = "Authorization"
description = "Bearer `$TOKEN`"
required = true
type = "string"

[[header]]
name = "Content-Type"
description = "application/json"
required = false
type = "string"
[/endpoint]

[responses]
[[response]]
status = 201
description = "Returns the newly created firewall rule."
body = '''
{
  "id": "example",
  "direction": "inbound",
  "protocol": "tcp",
  "ip_addresses": [
    "0.0.0.0/0",
    "::/0"
  ],
  "port_from": 443,
  "port_to": 443
}
'''

[[response]]
status = 400
description = "The body isn't valid JSON."
body = '''
{
  "error": {
    "message": "Malformed JSON body.",
    "code": "bad_request"
  }
}
'''

[[response]]
status = 401
description = "The API token is missing or invalid."
body = '''
{
  "error": {
    "message": "Missing or invalid token.",
    "code": "unauthorized"
  }
}
'''

[[response]]
status = 402
description = "The organization hasn't made its first payment yet, and the request creates a resource we charge for. The code is `payment_required`. A request that creates a server, load balancer, object cache, database, reserved IP or volume can also return `first_purchase`. The `url` in `first_purchase` opens a page where you make the payment. The resource is created after the payment. The page works until `expires_at`, 24 hours after the response."
body = '''
{
  "error": {
    "message": "We require at least one successful credit card transaction before allowing billable actions via the API. This significantly helps us to prevent fraud. Once your first transaction is processed, you will be able to use the API unrestricted. Please add credit to your account at https://betterstack.com/settings/billing/card/new?credits=10&activate_api=true. Need help? We're here for you at hello@betterstack.com.",
    "code": "payment_required"
  }
}
'''

[[response]]
status = 403
description = "The firewall has as many rules as the quota allows. The error code is `quota_exceeded`."

[[response]]
status = 404
description = "We couldn't find the firewall."

[[response]]
status = 409
description = "The resource's state doesn't allow the request, such as starting a running server or removing a placement group that still has servers."

[[response]]
status = 422
description = "A required field is missing, or a value isn't valid. The `ip_addresses` list must hold strings and can't be empty, and `port_to` can't be lower than `port_from`. The error code is `validation_failed`."

[[response]]
status = 423
description = "We've locked the resource, with the code `locked`. Every change to a locked resource is refused, and so is a change that involves one."

[[response]]
status = 429
description = "Too many requests, with the code `rate_limited`. We limit requests per client IP and per organization, and some costly endpoints have a stricter limit. Wait as long as the `Retry-After` header says."

[[response]]
status = 500
description = "An unexpected, temporary error on our side, with the code `internal_error`. Try again."
body = '''
{
  "error": {
    "message": "The request could not be completed right now. Please try again in a few moments.",
    "code": "internal_error"
  }
}
'''

[[response]]
status = 503
description = "Another rule is being added to this firewall at the same time. The error code is `concurrent_create_limit`. Try again in a few seconds."
[/responses]

[params]
title = "Response attributes"

[[param]]
name = "id"
type = "string"
description = "The rule's ID, a UUID."

[[param]]
name = "direction"
type = "string"
description = "Which traffic the rule matches.<br/>Valid values:<br/>`inbound`: The rule matches traffic that arrives at the server.<br/>`outbound`: The rule matches traffic that leaves the server."

[[param]]
name = "protocol"
type = "string"
description = "The IP protocol the rule matches. `any` matches every protocol. Possible values: `tcp`, `udp`, `icmp`, `gre`, `esp`, `any`."

[[param]]
name = "ip_addresses"
type = "array of strings"
description = "The addresses the rule applies to, in CIDR notation, such as `10.0.0.0/8`. Use `0.0.0.0/0` and `::/0` for every address. The list can't be empty."

[[param]]
name = "port_from"
type = "integer"
description = "The first port of the range, from `0` to `65535`. Every rule needs it. A protocol without ports, such as `icmp`, ignores it."

[[param]]
name = "port_to"
type = "integer or null"
description = "The last port of the range, from `port_from` to `65535`."
[/params]
