# Codes

The newest one-time code an address received.

An SDK signs every request with your key: [Authentication](https://mailset.app/docs/authentication.md). To sign one yourself, see [Calling the API without an SDK](https://mailset.app/docs/authentication/http.md).

## codes.latest

The newest one-time code mailed to an address you can read.

`POST /v1/codes.latest`

An app key with `read` on the mailbox. Or a person: any signed-in person, with an API token.

### Parameters

- `to` (string, required): The address the code was mailed to: a mailbox you can read, or an alias, +tag, catch-all or group that delivers into one.
- `from` (string, optional): Only mail from this sender: an address, or a domain with its subdomains (`yahoo.com`).
- `within` (integer or string, optional): How far back to look: `30m`, `2h`, `1d`, or minutes as a number. 30 minutes unless given, a day at most.

### Returns

`data` is an object with:

- `code` (string): The code, as it was mailed: 4 to 8 digits, or 6 to 8 capitals and digits.
- `from` (string): The sender’s address.
- `subject` (string): The subject of the message it came in.
- `receivedAt` (number): When the message arrived, in ms since the epoch.
- `mailboxAddress` (string): The mailbox the message is in.

### Request

TypeScript:

```typescript
import { Mailset } from 'mailset';

// Reads MAILSET_KEY from the environment.
const mailset = new Mailset();

const answer = await mailset.codes.latest({
	from: "yahoo.com",
	within: "10m",
	to: "ana@example.com",
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

# Reads MAILSET_KEY from the environment.
mailset = Mailset()

answer = mailset.codes.latest(
    {
        "from": "yahoo.com",
        "within": "10m",
        "to": "ana@example.com",
    }
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_KEY from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.CodesLatestParams{
		From:   mailset.String("yahoo.com"),
		Within: "10m",
		To:     "ana@example.com",
	}
	answer, err := client.Codes.Latest(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

# Reads MAILSET_KEY from the environment.
mailset = Mailset::Client.new

answer = mailset.codes.latest(
  from: 'yahoo.com',
  within: '10m',
  to: 'ana@example.com'
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();

$answer = $mailset->codes->latest([
    'from' => 'yahoo.com',
    'within' => '10m',
    'to' => 'ana@example.com',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.CodeFound;
import app.mailset.model.CodesLatestParams;

// Reads MAILSET_KEY from the environment.
Mailset mailset = Mailset.fromEnv();

var params = CodesLatestParams.builder()
        .from("yahoo.com")
        .within("10m")
        .to("ana@example.com")
        .build();
CodeFound answer = mailset.codes().latest(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::CodesLatestParams;

// Reads MAILSET_KEY from the environment.
let mailset = mailset::Client::from_env()?;

let answer = mailset
    .codes()
    .latest(CodesLatestParams {
        to: "ana@example.com".into(),
        from: Some("yahoo.com".into()),
        within: Some("10m".into()),
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/codes.latest \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"from":"yahoo.com","within":"10m","to":"ana@example.com"}}'
```

curl can’t sign a request. `$TOKEN` is your credential’s token, and `$MS_TIME` and `$MS_PROOF` sign this exact body: [Calling the API without an SDK](https://mailset.app/docs/authentication/http) shows how.

CLI:

```bash
mailset codes latest ana@example.com --from yahoo.com --within 10m
```

### Response

```json
{
  "ok": true,
  "data": {
    "code": "482913",
    "from": "hello@example.com",
    "subject": "Your Acme receipt",
    "receivedAt": 1796048940000,
    "mailboxAddress": "hello@example.com"
  }
}
```

In the CLI: [mailset reference](https://mailset.app/docs/cli/codes#codes-latest)

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content