---
title: "Authorized keys"
description: "Add or remove SSH public keys for a user."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.stackmachine.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authorized keys

Manage public keys for an SSH user: add a key, list keys, then remove by name when rotating credentials.

### Add an authorized key

Resolve the SSH user (list users if needed), then `create` with a `name` and `publicKey` / `public_key`.

```javascript
const users = await client.apps.ssh.users.list({ app: "da_XYZ", limit: 1 });
const user = users.data[0];

const key = await client.apps.ssh.users.authorizedKeys.create({
  user: user.id,
  name: "my-laptop",
  publicKey: "ssh-ed25519 AAAA... user@host",
});
// Should return: {
//   "id": "key_abc",
//   "name": "my-laptop",
//   "publicKey": "ssh-ed25519 AAAA... user@host",
//   "createdAt": "2024-01-15T12:00:00.000Z"
// }
console.log(key.id, key.publicKey);
```

```python
users = client.apps.ssh.users.list(app="da_XYZ", limit=1)
user = users.data[0]

key = client.apps.ssh.users.authorized_keys.create(
    user=user.id,
    name="my-laptop",
    public_key="ssh-ed25519 AAAA... user@host",
)
# Should return: {
#   "id": "key_abc",
#   "name": "my-laptop",
#   "public_key": "ssh-ed25519 AAAA... user@host"
# }
print(key.id, key.public_key)
```

### List keys

List authorized keys for the same `user` id to confirm the key was stored.

```javascript
const keys = await client.apps.ssh.users.authorizedKeys.list({
  user: user.id,
  limit: 10,
});
console.log(keys.data);
```

```python
keys = client.apps.ssh.users.authorized_keys.list(user=user.id, limit=10)
print(keys.data)
```

### Remove a key

Delete by `user` and key `name` when rotating or revoking access.

```javascript
await client.apps.ssh.users.authorizedKeys.del({ user: user.id, name: "my-laptop" });
console.log("Key removed");
```

```python
client.apps.ssh.users.authorized_keys.delete(user=user.id, name="my-laptop")
print("Key removed")
```

> Source: [stackmachine/sdks](https://github.com/stackmachine/sdks/tree/main) examples.

Source: https://docs.stackmachine.com/apps/ssh/authorized-keys/index.mdx
