# Create an SSH key

Create an SSH key in this zone. We copy it to every other zone in the background, shortly after the response. With `server_ids`, we also add the key to those servers that are running, in every zone.

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

[[body_param]]
name = "slug"
required = true
type = "string"
description = "The key's name, unique in its team. The name is up to 255 characters long and starts and ends with a letter or digit. Dots, hyphens and underscores are allowed in between. A deleted key's slug stays taken until the key is removed from every zone."

[[body_param]]
name = "public_key"
required = true
type = "string"
description = "Exactly one OpenSSH public key. Never send a private key. The CLI can read it from a file or from standard input, or import it from GitHub. The public key can't be changed after the key is created."

[[body_param]]
name = "team_id"
required = true
type = "string"
description = "The ID of the team that owns the key. `betterstack team list` shows it."

[[body_param]]
name = "server_ids"
type = "array of strings"
description = "The IDs of the servers to add the new key to, at most 100, in any zone. Only running servers get the key, and the key is created even when a server can't get it. Order doesn't matter."

[[body_param]]
name = "metadata"
type = "object"
description = "Your tags, as an object of string keys and values, at most 10. An update replaces every tag, so an empty object removes all tags. Accepts any keys."

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

[[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 organization has as many SSH keys as its quota allows. The code is `quota_exceeded`."

[[response]]
status = 404
description = "The team in `team_id` doesn't exist, or your token can't access it. The code is `not_found`."

[[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 = "The `slug` is taken in the team, `public_key` isn't a valid OpenSSH key, or `server_ids` isn't a list of at most 100 server IDs. The 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 = "A create with servers in `server_ids` counts toward the limit that attach and detach share, 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 in `server_ids`, returned only when you sent it. Each server's events show whether adding the key worked."

[[param]]
name = "servers.queued"
type = "array of strings"
description = "The IDs of the servers of this zone we're adding the key to."

[[param]]
name = "servers.skipped"
type = "array of objects"
description = "The servers of this zone that can't get the key now, each with a reason."

[[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."

[[param]]
name = "servers.forwarded"
type = "array of strings"
description = "The IDs of the servers in other zones. The key is added to each of them once it has been copied to the server's zone."
[/params]
