# Attach an SSH key

Add the key to the root user's `authorized_keys` on running servers in this zone. We add it in the background, usually within seconds. Each server records `ssh_key_attached` or `ssh_key_attach_failed` in its events. The key shows in the server's `ssh_keys` at once, and a later restore or rescue keeps it. Attaching a key a server already has adds it again.

[endpoint]
method = "POST"
base_url = "https://{availability_zone}.betterstackcloud.com"
path = "/v1/ssh-keys/{slug}/attach"

[[path_param]]
name = "slug"
required = true
type = "string"
description = "The `slug` of the key. Dots are part of the slug."

[[query_param]]
name = "team_id"
type = "string"
description = "The ID of the team that owns the key. Send it when more than one of your teams has a key with this `slug`."

[[body_param]]
name = "server_ids"
type = "array of strings"
description = "The IDs of the servers to attach the key to. When one of them doesn't exist or can't get the key now, the request is refused and nothing changes. A repeated ID counts once."

[[body_param]]
name = "all"
type = "boolean"
description = "Whether to attach the key to every server in this zone. A server that can't get the key now is skipped and listed with a reason. Send either `all` or `server_ids`, never both."

[[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 = 202
description = "Returns the SSH key."
body = '''
{
  "servers": {
    "queued": [
      "example"
    ],
    "skipped": [
      {
        "server_id": "123456789",
        "reason": "kubernetes_managed"
      }
    ]
  }
}
'''

[[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 = "No key with this `slug` exists in your teams, or a server in `server_ids` doesn't exist or your token can't access it. The code is `not_found`."

[[response]]
status = 409
description = "A server in `server_ids` isn't running, with the code `server_not_running`. Or it's a Kubernetes worker node, with the code `kubernetes_managed`."

[[response]]
status = 422
description = "The request has neither or both of `server_ids` and `all`, or `server_ids` isn't a list of server IDs with at least one, with the code `validation_failed`. Or more than one of your teams has a key with this `slug` and the request has no `team_id`, with the code `team_required`."

[[response]]
status = 423
description = "A server in `server_ids` is locked. The code is `locked`."

[[response]]
status = 429
description = "Attach and detach share a limit of 30 requests per organization per minute. The code is `rate_limited`."

[[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 = "servers"
type = "object"
description = "What we did with each server."

[[param]]
name = "servers.queued"
type = "array of strings"
description = "The IDs of the servers we're changing."

[[param]]
name = "servers.skipped"
type = "array of objects"
description = "The servers we skipped with `all`, each with a reason, empty when you send `server_ids`."

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

[[param]]
name = "servers.skipped[].reason"
type = "string"
description = "Why we skipped the server.<br/>Valid values:<br/>`kubernetes_managed`: The server is a Kubernetes worker node.<br/>`locked`: The server is locked.<br/>`server_not_running`: The server isn't running."
[/params]
