# Create a load balancer service

Add a service to a load balancer. A running load balancer starts serving it a few moments after the response.

[endpoint]
method = "POST"
base_url = "https://{availability_zone}.betterstackcloud.com"
path = "/v1/load-balancers/{load_balancer_id}/services"

[[path_param]]
name = "load_balancer_id"
required = true
type = "string"
description = "The ID of the load balancer."

[[body_param]]
name = "protocol"
required = true
type = "string"
description = "The protocol the service listens for.<br/>Valid values:<br/>`tcp`: Forwards TCP connections.<br/>`http`: Forwards HTTP requests.<br/>`https`: Ends TLS with the service's certificates and forwards HTTP requests. An `https` service requires at least one certificate in `certificate_ids`.<br/>`udp`: Forwards UDP traffic. A `udp` service can't use certificates, `sticky_sessions`, `redirect_http` or `proxy_protocol`."

[[body_param]]
name = "listen_port"
required = true
type = "integer"
description = "The port the service listens on, 1–65535. Can't be `8402` or `9901`, which the load balancer uses itself. A `tcp` and a `udp` service can use the same port."

[[body_param]]
name = "destination_port"
required = true
type = "integer"
description = "The port on the targets that traffic goes to, 1–65535."

[[body_param]]
name = "redirect_http"
default = "false"
type = "boolean"
description = "Whether HTTP requests are redirected to this HTTPS service. Only `https` services can redirect."

[[body_param]]
name = "http_timeout_seconds"
default = "50"
type = "integer"
description = "The HTTP timeout in seconds, 5–600."

[[body_param]]
name = "sticky_sessions"
default = "false"
type = "boolean"
description = "Whether a cookie sends each client to the same target. Only `http` and `https` services can use sticky sessions."

[[body_param]]
name = "sticky_cookie_name"
type = "string"
description = "The name of the sticky session cookie, 1–64 letters, digits, underscores, dots or hyphens. Defaults to `BSLBSTICKY`."

[[body_param]]
name = "sticky_cookie_lifetime_seconds"
default = "300"
type = "integer"
description = "How long the sticky session cookie lasts, 1–86400 seconds."

[[body_param]]
name = "proxy_protocol"
default = "disabled"
type = "string"
description = "The PROXY protocol version the targets accept, or `disabled`. A `udp` service can't use it. Possible values: `disabled`, `v1`, `v2`."

[[body_param]]
name = "certificate_ids"
type = "array of strings"
description = "The IDs of the certificates the service presents. Each certificate must be in the load balancer's team. An `https` service requires at least one certificate. An update replaces the whole list."

[[body_param]]
name = "health_check"
type = "object"
description = "The health check of the service's targets. An update changes only the health check fields you send."

[[body_param]]
name = "health_check.protocol"
type = "string"
description = "How we check the targets.<br/>Valid values:<br/>`tcp`: Checks that the target accepts a TCP connection. The default for `tcp` and `udp` services.<br/>`http`: Sends an HTTP request to the target. The default for `http` and `https` services. On a `tcp` or `udp` service, an `http` check needs its own `port`, different from `destination_port`."

[[body_param]]
name = "health_check.port"
type = "integer or null"
description = "The port we check, 1–65535. Without it, we check `destination_port`."

[[body_param]]
name = "health_check.interval_seconds"
default = "15"
type = "integer"
description = "How often we check each target, 2–3600 seconds."

[[body_param]]
name = "health_check.timeout_seconds"
default = "10"
type = "integer"
description = "The check timeout in seconds, 1–60."

[[body_param]]
name = "health_check.retries"
default = "3"
type = "integer"
description = "How many checks in a row must fail before a target is down, 1–10."

[[body_param]]
name = "health_check.domain"
type = "string or null"
description = "The hostname we send in the `Host` header of an HTTP check."

[[body_param]]
name = "health_check.path"
default = "/"
type = "string"
description = "The path an HTTP check requests, starting with `/`, with no spaces or quotes."

[[body_param]]
name = "health_check.response"
type = "string or null"
description = "Text the response must contain, at most 255 characters with no line breaks or quotes."

[[body_param]]
name = "health_check.status_codes"
default = "2??,3??"
type = "string"
description = "The status codes that pass, separated by commas. In a pattern, `?` matches one digit and `*` any number of digits."

[[body_param]]
name = "health_check.tls"
default = "false"
type = "boolean"
description = "Whether checks connect with TLS."

[[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 load balancer service."
body = '''
{
  "id": 1,
  "protocol": "tcp",
  "listen_port": 80,
  "destination_port": 8080,
  "redirect_http": false,
  "http_timeout_seconds": 50,
  "sticky_sessions": false,
  "sticky_cookie_name": "Example load balancer service",
  "sticky_cookie_lifetime_seconds": 300,
  "proxy_protocol": "disabled",
  "certificate_ids": [
    "123456789"
  ],
  "health_check": {
    "protocol": "tcp",
    "port": 443,
    "interval_seconds": 15,
    "timeout_seconds": 10,
    "retries": 3,
    "domain": "example",
    "path": "/",
    "response": "example",
    "status_codes": "2??,3??",
    "tls": false
  },
  "target_health": [
    {
      "service_id": 123456789,
      "server_id": "123456789",
      "state": "up",
      "state_changed_at": "2026-07-01T12:00:00.000Z",
      "status_message": "example"
    }
  ],
  "created_at": "2026-07-01T12:00:00.000Z",
  "updated_at": "2026-07-01T12:00:00.000Z"
}
'''

[[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 request would go over a quota, with the code `quota_exceeded`. Or a read-only token tried to make a change, with the code `read_only_token`. Or a rule of the resource forbids it."
body = '''
{
  "error": {
    "message": "This token is read-only and cannot be used for write operations.",
    "code": "read_only_token"
  }
}
'''

[[response]]
status = 404
description = "The resource doesn't exist, it's in another zone, or your token can't access it."
body = '''
{
  "error": {
    "message": "Resource not found.",
    "code": "not_found"
  }
}
'''

[[response]]
status = 409
description = "The load balancer has as many services as its type allows."

[[response]]
status = 409
description = "The load balancer would present more certificates than its type allows."

[[response]]
status = 409
description = "A Kubernetes cluster manages the load balancer."

[[response]]
status = 422
description = "A value isn't valid, with the code `validation_failed`. The error's `details` list the messages for each field. A create or rescale that names a deprecated type with a replacement returns the code `type_deprecated`. The error's `details` then name the field, the deprecated type and its `replacement`."

[[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 = "The zone has no capacity, with the code `no_capacity`. Or we couldn't set up or change the resource, with the code `orchestration_failed`. Or the organization is creating too many resources at the same time, with the code `concurrent_create_limit`. Try again after a short wait."
[/responses]

[params]
title = "Response attributes"

[[param]]
name = "id"
type = "integer"
description = "The service's ID."

[[param]]
name = "protocol"
type = "string"
description = "The protocol the service listens for.<br/>Valid values:<br/>`tcp`: Forwards TCP connections.<br/>`http`: Forwards HTTP requests.<br/>`https`: Ends TLS with the service's certificates and forwards HTTP requests. An `https` service requires at least one certificate in `certificate_ids`.<br/>`udp`: Forwards UDP traffic. A `udp` service can't use certificates, `sticky_sessions`, `redirect_http` or `proxy_protocol`."

[[param]]
name = "listen_port"
type = "integer"
description = "The port the service listens on, 1–65535. Can't be `8402` or `9901`, which the load balancer uses itself. A `tcp` and a `udp` service can use the same port."

[[param]]
name = "destination_port"
type = "integer"
description = "The port on the targets that traffic goes to, 1–65535."

[[param]]
name = "redirect_http"
type = "boolean"
description = "Whether HTTP requests are redirected to this HTTPS service. Only `https` services can redirect."

[[param]]
name = "http_timeout_seconds"
type = "integer"
description = "The HTTP timeout in seconds, 5–600."

[[param]]
name = "sticky_sessions"
type = "boolean"
description = "Whether a cookie sends each client to the same target. Only `http` and `https` services can use sticky sessions."

[[param]]
name = "sticky_cookie_name"
type = "string"
description = "The name of the sticky session cookie, 1–64 letters, digits, underscores, dots or hyphens."

[[param]]
name = "sticky_cookie_lifetime_seconds"
type = "integer"
description = "How long the sticky session cookie lasts, 1–86400 seconds."

[[param]]
name = "proxy_protocol"
type = "string"
description = "The PROXY protocol version the targets accept, or `disabled`. A `udp` service can't use it. Possible values: `disabled`, `v1`, `v2`."

[[param]]
name = "certificate_ids"
type = "array of strings"
description = "The IDs of the certificates the service presents."

[[param]]
name = "health_check"
type = "object"
description = "The health check of the service's targets."

[[param]]
name = "health_check.protocol"
type = "string"
description = "How we check the targets.<br/>Valid values:<br/>`tcp`: Checks that the target accepts a TCP connection. The default for `tcp` and `udp` services.<br/>`http`: Sends an HTTP request to the target. The default for `http` and `https` services. On a `tcp` or `udp` service, an `http` check needs its own `port`, different from `destination_port`."

[[param]]
name = "health_check.port"
type = "integer or null"
description = "The port we check, 1–65535. Without it, we check `destination_port`."

[[param]]
name = "health_check.interval_seconds"
type = "integer"
description = "How often we check each target, 2–3600 seconds."

[[param]]
name = "health_check.timeout_seconds"
type = "integer"
description = "The check timeout in seconds, 1–60."

[[param]]
name = "health_check.retries"
type = "integer"
description = "How many checks in a row must fail before a target is down, 1–10."

[[param]]
name = "health_check.domain"
type = "string or null"
description = "The hostname we send in the `Host` header of an HTTP check."

[[param]]
name = "health_check.path"
type = "string"
description = "The path an HTTP check requests, starting with `/`, with no spaces or quotes."

[[param]]
name = "health_check.response"
type = "string or null"
description = "Text the response must contain, at most 255 characters with no line breaks or quotes."

[[param]]
name = "health_check.status_codes"
type = "string"
description = "The status codes that pass, separated by commas. In a pattern, `?` matches one digit and `*` any number of digits."

[[param]]
name = "health_check.tls"
type = "boolean"
description = "Whether checks connect with TLS."

[[param]]
name = "target_health"
type = "array of objects"
description = "The latest health of each target, updated about every 30 seconds."

[[param]]
name = "target_health[].service_id"
type = "integer"
description = "The ID of the service."

[[param]]
name = "target_health[].server_id"
type = "string"
description = "The ID of the target server."

[[param]]
name = "target_health[].state"
type = "string"
description = "Whether the target is healthy.<br/>Valid values:<br/>`up`: The target passes its checks.<br/>`down`: The target fails its checks.<br/>`unknown`: We have no result for the target yet."

[[param]]
name = "target_health[].state_changed_at"
type = "datetime or null"
description = "When `state` last changed, or `null`. In ISO 8601 format, such as `2026-07-01T12:00:00Z`."

[[param]]
name = "target_health[].status_message"
type = "string or null"
description = "Why the target is down, or `null`."

[[param]]
name = "created_at"
type = "datetime"
description = "When the service was created. In ISO 8601 format, such as `2026-07-01T12:00:00Z`."

[[param]]
name = "updated_at"
type = "datetime"
description = "When the service was last updated. In ISO 8601 format, such as `2026-07-01T12:00:00Z`."
[/params]
