# Enroll a new node

`POST https://api.mailmasker.org/api/v1/admin/agent/enroll`

Get a one-time code that lets a new node's agent sign itself in.

- Who can use it: Site admins
- Area: Fleet and status
- Answers 201 Created when it works
- Page: https://developer.mailmasker.org/admin-enroll-node

Do this before installing homelab-agent on the new node. The code is tied to the host name you give. The agent there trades it for its own sign-in, then checks in by itself about once a minute. Enrolling a host name again later (after its agent has been offline too long, say) keeps the same identity.

## Parameters

| Name | In | Required | What it is | Example |
|---|---|---|---|---|
| `hostname` | body | yes | The node's name, exactly as its agent is set up with it. | `prod-homelab12` |
| `ttl_minutes` | body | no | How many minutes the code stays usable. The default is 10. |  |

## Good to know

- The code works once, and only until it expires.
- Hand it over by preseeding the homelab-agent/enrollment_code answer before installing the agent; homelab-cli prints the exact line to run.

## Examples

cURL:

```bash
curl -s -X POST 'https://api.mailmasker.org/api/v1/admin/agent/enroll' \
  -H "Authorization: Bearer $TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{"hostname":"prod-homelab12"}'
```

Perl:

```perl
use Mojo::Base -strict;
use Mojo::UserAgent;

my $ua  = Mojo::UserAgent->new;
my $res = $ua->post('https://api.mailmasker.org/api/v1/admin/agent/enroll',
    { Authorization => "Bearer $ENV{TOKEN}" },
    json => { hostname => 'prod-homelab12' })->result;
die $res->code, ' ', ($res->json('/error') // $res->message), "\n" unless $res->is_success;
print $res->body, "\n";
```

homelab-cli:

```bash
homelab-cli admin agent enroll prod-homelab12
```

## Response

201 Created:

```json
{ "code": "<one-time token>", "hostname": "prod-homelab12", "expires_in_minutes": 10 }
```
