# Rules

File, label or forward new email as it arrives.

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).

## The Rule object

A rule on a mailbox: the email it matches and what it does to it.

### Attributes

- `id` (string): The rule’s id.
- `mailbox` (string): The mailbox it runs on.
- `name` (string): The rule’s name.
- `conditions` (object): What mail it matches. Every part has to hold.
  - `from` (string, optional): Mail from this sender.
  - `to` (string, optional): Mail sent to this address.
  - `subject` (string, optional): Words in the subject.
  - `hasWords` (string, optional): Words anywhere in it.
  - `doesntHave` (string, optional): Words it must not have.
  - `hasAttachment` (boolean, optional): It carries a file.
  - `larger` (number, optional): Larger than this many bytes.
  - `smaller` (number, optional): Smaller than this many bytes.
- `actions` (object): What it does to that mail.
  - `skipInbox` (boolean, optional): Archives it as it arrives.
  - `markRead` (boolean, optional): Marks it read.
  - `star` (boolean, optional): Stars it.
  - `label` (string, optional): Puts this label on it.
  - `forward` (string, optional): Forwards it to this address.
  - `delete` (boolean, optional): Moves it to Trash.
  - `neverJunk` (boolean, optional): Never sends it to Junk.
  - `important` (boolean, optional): Marks the conversation important.
  - `neverImportant` (boolean, optional): Keeps it from ever being marked important. Beats `important`.
  - `tab` (enum, optional): Files it in this inbox tab. One of `primary`, `updates`, `newsletters`, `promotions`.
- `summary` (string): The rule in one line, in the operators it’s written in.
- `matched` (number): Conversations it has matched or changed.
- `run` (nullable object): Its latest run over existing mail, if it has had one.
  - `id` (string): The run’s id.
  - `state` (enum): `running`, `done` or `failed`. One of `running`, `done`, `failed`.
  - `scanned` (number): Conversations looked at so far.
  - `changed` (number): Of those, how many the rule changed.
  - `reason` (nullable string)
- `forwardWaiting` (boolean): It forwards, and forwarding isn’t on here yet.

```json
{
  "id": "out_3f9a1c0e7b2d",
  "mailbox": "text",
  "name": "Acme API",
  "conditions": {
    "from": "hello@example.com",
    "to": "ana@example.com",
    "subject": "Your Acme receipt",
    "hasWords": "text",
    "doesntHave": "text",
    "hasAttachment": true,
    "larger": 1,
    "smaller": 1
  },
  "actions": {
    "skipInbox": true,
    "markRead": true,
    "star": true,
    "label": "text",
    "forward": "text",
    "delete": true,
    "neverJunk": true,
    "important": true,
    "neverImportant": true,
    "tab": "primary"
  },
  "summary": "text",
  "matched": 1,
  "run": {
    "id": "out_3f9a1c0e7b2d",
    "state": "running",
    "scanned": 1,
    "changed": 1,
    "reason": "The day’s sending limit is reached."
  },
  "forwardWaiting": true
}
```

Answered by [rules.list](https://mailset.app/docs/api/rules#rules.list), [rules.add](https://mailset.app/docs/api/rules#rules.add), [rules.update](https://mailset.app/docs/api/rules#rules.update), [rules.reorder](https://mailset.app/docs/api/rules#rules.reorder).

## rules.list

List the rules on your mailboxes, in the order they run.

`POST /v1/rules.list`

Any signed-in person, with an API token.

### Parameters

- `mailbox` (string, optional): Only this mailbox’s rules.

### Returns

`data` is a list of [Rule objects](https://mailset.app/docs/api/rules#rule).

### Request

TypeScript:

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

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

const answer = await mailset.rules.list({
	mailbox: "ana@example.com",
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.list(
    {"mailbox": "ana@example.com"}
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesListParams{
		Mailbox: mailset.String("ana@example.com"),
	}
	answer, err := client.Rules.List(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.list(mailbox: 'ana@example.com')
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->list([
    'mailbox' => 'ana@example.com',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.Rule;
import app.mailset.model.RulesListParams;
import java.util.List;

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

var params = RulesListParams.builder()
        .mailbox("ana@example.com")
        .build();
List<Rule> answer = mailset.rules().list(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesListParams;

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

let answer = mailset
    .rules()
    .list(RulesListParams {
        mailbox: Some("ana@example.com".into()),
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.list \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"mailbox":"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 rules list --mailbox ana@example.com
```

### Response

```json
{
  "ok": true,
  "data": [
    {
      "id": "out_3f9a1c0e7b2d",
      "mailbox": "text",
      "name": "Acme API",
      "conditions": {
        "from": "hello@example.com",
        "to": "ana@example.com",
        "subject": "Your Acme receipt",
        "hasWords": "text",
        "doesntHave": "text",
        "hasAttachment": true,
        "larger": 1,
        "smaller": 1
      },
      "actions": {
        "skipInbox": true,
        "markRead": true,
        "star": true,
        "label": "text",
        "forward": "text",
        "delete": true,
        "neverJunk": true,
        "important": true,
        "neverImportant": true,
        "tab": "primary"
      },
      "summary": "text",
      "matched": 1,
      "run": {
        "id": "out_3f9a1c0e7b2d",
        "state": "running",
        "scanned": 1,
        "changed": 1,
        "reason": "The day’s sending limit is reached."
      },
      "forwardWaiting": true
    }
  ]
}
```

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

## rules.add

Add a rule.

`POST /v1/rules.add`

Any signed-in person, with an API token. It can stop for a passkey approval.

### Parameters

- `mailbox` (string, required): The mailbox the rule runs on.
- `name` (string, optional): A name for the rule, up to 120 characters. At most 120 characters.
- `from` (string, optional): Mail from this sender: an address, a domain or a name. At most 1,500 characters.
- `to` (string, optional): Mail sent to this address. At most 1,500 characters.
- `subject` (string, optional): Mail with these words in its subject. At most 1,500 characters.
- `hasWords` (string, optional): Mail with these words anywhere in it. At most 1,500 characters.
- `doesntHave` (string, optional): Mail without these words. At most 1,500 characters.
- `hasAttachment` (boolean, optional): Mail that carries a file.
- `larger` (integer or string, optional): Mail larger than this: bytes, or `5M`, `500K`.
- `smaller` (integer or string, optional): Mail smaller than this: bytes, or `5M`, `500K`.
- `skipInbox` (boolean, optional): Archive it as it arrives.
- `markRead` (boolean, optional): Mark it read.
- `star` (boolean, optional): Star it.
- `label` (string or enum, optional): Put this label on it, by name.
- `forward` (string or enum, optional): Forward it to this address. Asks for a passkey.
- `delete` (boolean, optional): Move it to Trash.
- `neverJunk` (boolean, optional): Never send it to Junk.
- `important` (boolean, optional): Mark it important.
- `neverImportant` (boolean, optional): Never mark it important. Beats `important`.
- `tab` (enum, optional): File it in this inbox tab. One of `primary`, `updates`, `newsletters`, `promotions`, ``.
- `applyExisting` (boolean, optional): Also run it over the conversations already here.

Beside `input`:

- `approval` (string, optional): An approval’s id, once someone approved it with their passkey. Without one, the answer is `step_up_required` and names what to approve.

### Returns

`data` is the [Rule object](https://mailset.app/docs/api/rules#rule).

### Request

TypeScript:

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

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

const answer = await mailset.rules.add({
	mailbox: "ana@example.com",
	from: "stripe.com",
	label: "Finance",
	skipInbox: true,
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.add(
    {
        "mailbox": "ana@example.com",
        "from": "stripe.com",
        "label": "Finance",
        "skip_inbox": True,
    }
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesAddParams{
		Mailbox:   "ana@example.com",
		From:      mailset.String("stripe.com"),
		Label:     mailset.String("Finance"),
		SkipInbox: mailset.Bool(true),
	}
	answer, err := client.Rules.Add(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.add(
  mailbox: 'ana@example.com',
  from: 'stripe.com',
  label: 'Finance',
  skip_inbox: true
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->add([
    'mailbox' => 'ana@example.com',
    'from' => 'stripe.com',
    'label' => 'Finance',
    'skipInbox' => true,
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.Rule;
import app.mailset.model.RulesAddParams;

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

var params = RulesAddParams.builder()
        .mailbox("ana@example.com")
        .from("stripe.com")
        .label("Finance")
        .skipInbox(true)
        .build();
Rule answer = mailset.rules().add(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesAddParams;

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

let answer = mailset
    .rules()
    .add(RulesAddParams {
        mailbox: "ana@example.com".into(),
        from: Some("stripe.com".into()),
        skip_inbox: Some(true),
        label: Some("Finance".into()),
        ..Default::default()
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.add \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"mailbox":"ana@example.com","from":"stripe.com","label":"Finance","skipInbox":true}}'
```

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 rules add --mailbox ana@example.com --from stripe.com --label Finance --skip-inbox
```

### Response

```json
{
  "ok": true,
  "data": {
    "id": "out_3f9a1c0e7b2d",
    "mailbox": "text",
    "name": "Acme API",
    "conditions": {
      "from": "hello@example.com",
      "to": "ana@example.com",
      "subject": "Your Acme receipt",
      "hasWords": "text",
      "doesntHave": "text",
      "hasAttachment": true,
      "larger": 1,
      "smaller": 1
    },
    "actions": {
      "skipInbox": true,
      "markRead": true,
      "star": true,
      "label": "text",
      "forward": "text",
      "delete": true,
      "neverJunk": true,
      "important": true,
      "neverImportant": true,
      "tab": "primary"
    },
    "summary": "text",
    "matched": 1,
    "run": {
      "id": "out_3f9a1c0e7b2d",
      "state": "running",
      "scanned": 1,
      "changed": 1,
      "reason": "The day’s sending limit is reached."
    },
    "forwardWaiting": true
  }
}
```

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

## rules.update

Change a rule.

`POST /v1/rules.update`

Any signed-in person, with an API token. It can stop for a passkey approval.

### Parameters

- `id` (string, required): The rule’s id, from `rules list`.
- `name` (string, optional): A name for the rule, up to 120 characters. At most 120 characters.
- `from` (string, optional): Mail from this sender: an address, a domain or a name. At most 1,500 characters.
- `to` (string, optional): Mail sent to this address. At most 1,500 characters.
- `subject` (string, optional): Mail with these words in its subject. At most 1,500 characters.
- `hasWords` (string, optional): Mail with these words anywhere in it. At most 1,500 characters.
- `doesntHave` (string, optional): Mail without these words. At most 1,500 characters.
- `hasAttachment` (boolean, optional): Mail that carries a file.
- `larger` (integer or string, optional): Mail larger than this: bytes, or `5M`, `500K`.
- `smaller` (integer or string, optional): Mail smaller than this: bytes, or `5M`, `500K`.
- `skipInbox` (boolean, optional): Archive it as it arrives.
- `markRead` (boolean, optional): Mark it read.
- `star` (boolean, optional): Star it.
- `label` (string or enum, optional): Put this label on it, by name.
- `forward` (string or enum, optional): Forward it to this address. Asks for a passkey.
- `delete` (boolean, optional): Move it to Trash.
- `neverJunk` (boolean, optional): Never send it to Junk.
- `important` (boolean, optional): Mark it important.
- `neverImportant` (boolean, optional): Never mark it important. Beats `important`.
- `tab` (enum, optional): File it in this inbox tab. One of `primary`, `updates`, `newsletters`, `promotions`, ``.
- `applyExisting` (boolean, optional): Also run it over the conversations already here.

Beside `input`:

- `approval` (string, optional): An approval’s id, once someone approved it with their passkey. Without one, the answer is `step_up_required` and names what to approve.

### Returns

`data` is the [Rule object](https://mailset.app/docs/api/rules#rule).

### Request

TypeScript:

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

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

const answer = await mailset.rules.update({
	label: "Invoices",
	markRead: true,
	id: "rl_1c7e3a9f5b2d4068e4a9c2f1",
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.update(
    {
        "label": "Invoices",
        "mark_read": True,
        "id": "rl_1c7e3a9f5b2d4068e4a9c2f1",
    }
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesUpdateParams{
		Label:    mailset.String("Invoices"),
		MarkRead: mailset.Bool(true),
		ID:       "rl_1c7e3a9f5b2d4068e4a9c2f1",
	}
	answer, err := client.Rules.Update(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.update(
  label: 'Invoices',
  mark_read: true,
  id: 'rl_1c7e3a9f5b2d4068e4a9c2f1'
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->update([
    'label' => 'Invoices',
    'markRead' => true,
    'id' => 'rl_1c7e3a9f5b2d4068e4a9c2f1',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.Rule;
import app.mailset.model.RulesUpdateParams;

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

var params = RulesUpdateParams.builder()
        .label("Invoices")
        .markRead(true)
        .id("rl_1c7e3a9f5b2d4068e4a9c2f1")
        .build();
Rule answer = mailset.rules().update(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesUpdateParams;

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

let answer = mailset
    .rules()
    .update(RulesUpdateParams {
        id: "rl_1c7e3a9f5b2d4068e4a9c2f1".into(),
        mark_read: Some(true),
        label: Some("Invoices".into()),
        ..Default::default()
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.update \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"label":"Invoices","markRead":true,"id":"rl_1c7e3a9f5b2d4068e4a9c2f1"}}'
```

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 rules update rl_1c7e3a9f5b2d4068e4a9c2f1 --label Invoices --mark-read
```

### Response

```json
{
  "ok": true,
  "data": {
    "id": "out_3f9a1c0e7b2d",
    "mailbox": "text",
    "name": "Acme API",
    "conditions": {
      "from": "hello@example.com",
      "to": "ana@example.com",
      "subject": "Your Acme receipt",
      "hasWords": "text",
      "doesntHave": "text",
      "hasAttachment": true,
      "larger": 1,
      "smaller": 1
    },
    "actions": {
      "skipInbox": true,
      "markRead": true,
      "star": true,
      "label": "text",
      "forward": "text",
      "delete": true,
      "neverJunk": true,
      "important": true,
      "neverImportant": true,
      "tab": "primary"
    },
    "summary": "text",
    "matched": 1,
    "run": {
      "id": "out_3f9a1c0e7b2d",
      "state": "running",
      "scanned": 1,
      "changed": 1,
      "reason": "The day’s sending limit is reached."
    },
    "forwardWaiting": true
  }
}
```

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

## rules.remove

Delete a rule.

`POST /v1/rules.remove`

Any signed-in person, with an API token.

### Parameters

- `id` (string, required): The rule’s id, from `rules list`.

### Returns

`data` is an object with:

- `removed` (boolean)

### Request

TypeScript:

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

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

const answer = await mailset.rules.remove({
	id: "rl_1c7e3a9f5b2d4068e4a9c2f1",
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.remove(
    {"id": "rl_1c7e3a9f5b2d4068e4a9c2f1"}
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesRemoveParams{
		ID: "rl_1c7e3a9f5b2d4068e4a9c2f1",
	}
	answer, err := client.Rules.Remove(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.remove(
  id: 'rl_1c7e3a9f5b2d4068e4a9c2f1'
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->remove([
    'id' => 'rl_1c7e3a9f5b2d4068e4a9c2f1',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.RulesRemoveParams;
import app.mailset.model.RulesRemoveResult;

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

var params = RulesRemoveParams.builder()
        .id("rl_1c7e3a9f5b2d4068e4a9c2f1")
        .build();
RulesRemoveResult answer = mailset.rules().remove(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesRemoveParams;

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

let answer = mailset
    .rules()
    .remove(RulesRemoveParams {
        id: "rl_1c7e3a9f5b2d4068e4a9c2f1".into(),
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.remove \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"id":"rl_1c7e3a9f5b2d4068e4a9c2f1"}}'
```

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 rules remove rl_1c7e3a9f5b2d4068e4a9c2f1
```

### Response

```json
{
  "ok": true,
  "data": {
    "removed": true
  }
}
```

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

## rules.reorder

Set the order a mailbox’s rules run in.

`POST /v1/rules.reorder`

Any signed-in person, with an API token.

### Parameters

- `mailbox` (string, required): The mailbox whose rules these are.
- `ids` (array of strings, required): Every rule’s id, in the order they should run. 1 to 1,000 items.

### Returns

`data` is a list of [Rule objects](https://mailset.app/docs/api/rules#rule).

### Request

TypeScript:

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

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

const answer = await mailset.rules.reorder({
	mailbox: "ana@example.com",
	ids: [
		"rl_9a2f6c1e8b4d3057c1e6a3b8",
		"rl_1c7e3a9f5b2d4068e4a9c2f1",
	],
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.reorder(
    {
        "mailbox": "ana@example.com",
        "ids": [
            "rl_9a2f6c1e8b4d3057c1e6a3b8",
            "rl_1c7e3a9f5b2d4068e4a9c2f1",
        ],
    }
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesReorderParams{
		Mailbox: "ana@example.com",
		IDs: []string{
			"rl_9a2f6c1e8b4d3057c1e6a3b8",
			"rl_1c7e3a9f5b2d4068e4a9c2f1",
		},
	}
	answer, err := client.Rules.Reorder(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.reorder(
  mailbox: 'ana@example.com',
  ids: [
    'rl_9a2f6c1e8b4d3057c1e6a3b8',
    'rl_1c7e3a9f5b2d4068e4a9c2f1'
  ]
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->reorder([
    'mailbox' => 'ana@example.com',
    'ids' => [
        'rl_9a2f6c1e8b4d3057c1e6a3b8',
        'rl_1c7e3a9f5b2d4068e4a9c2f1',
    ],
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.Rule;
import app.mailset.model.RulesReorderParams;
import java.util.List;

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

var params = RulesReorderParams.builder()
        .mailbox("ana@example.com")
        .ids(List.of(
                "rl_9a2f6c1e8b4d3057c1e6a3b8",
                "rl_1c7e3a9f5b2d4068e4a9c2f1"))
        .build();
List<Rule> answer = mailset.rules().reorder(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesReorderParams;

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

let answer = mailset
    .rules()
    .reorder(RulesReorderParams {
        mailbox: "ana@example.com".into(),
        ids: vec![
            "rl_9a2f6c1e8b4d3057c1e6a3b8".into(),
            "rl_1c7e3a9f5b2d4068e4a9c2f1".into(),
        ],
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.reorder \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"mailbox":"ana@example.com","ids":["rl_9a2f6c1e8b4d3057c1e6a3b8","rl_1c7e3a9f5b2d4068e4a9c2f1"]}}'
```

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 rules reorder --mailbox ana@example.com --ids rl_9a2f6c1e8b4d3057c1e6a3b8,rl_1c7e3a9f5b2d4068e4a9c2f1
```

### Response

```json
{
  "ok": true,
  "data": [
    {
      "id": "out_3f9a1c0e7b2d",
      "mailbox": "text",
      "name": "Acme API",
      "conditions": {
        "from": "hello@example.com",
        "to": "ana@example.com",
        "subject": "Your Acme receipt",
        "hasWords": "text",
        "doesntHave": "text",
        "hasAttachment": true,
        "larger": 1,
        "smaller": 1
      },
      "actions": {
        "skipInbox": true,
        "markRead": true,
        "star": true,
        "label": "text",
        "forward": "text",
        "delete": true,
        "neverJunk": true,
        "important": true,
        "neverImportant": true,
        "tab": "primary"
      },
      "summary": "text",
      "matched": 1,
      "run": {
        "id": "out_3f9a1c0e7b2d",
        "state": "running",
        "scanned": 1,
        "changed": 1,
        "reason": "The day’s sending limit is reached."
      },
      "forwardWaiting": true
    }
  ]
}
```

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

## rules.test

Show what a rule would match, without doing anything.

`POST /v1/rules.test`

Any signed-in person, with an API token.

### Parameters

- `id` (string, optional): A rule you have, by its id. Left out: the flags describe one.
- `mailbox` (string, optional): The mailbox to test against, for a rule described by flags.
- `name` (string, optional): A name for the rule, up to 120 characters. At most 120 characters.
- `from` (string, optional): Mail from this sender: an address, a domain or a name. At most 1,500 characters.
- `to` (string, optional): Mail sent to this address. At most 1,500 characters.
- `subject` (string, optional): Mail with these words in its subject. At most 1,500 characters.
- `hasWords` (string, optional): Mail with these words anywhere in it. At most 1,500 characters.
- `doesntHave` (string, optional): Mail without these words. At most 1,500 characters.
- `hasAttachment` (boolean, optional): Mail that carries a file.
- `larger` (integer or string, optional): Mail larger than this: bytes, or `5M`, `500K`.
- `smaller` (integer or string, optional): Mail smaller than this: bytes, or `5M`, `500K`.
- `skipInbox` (boolean, optional): Archive it as it arrives.
- `markRead` (boolean, optional): Mark it read.
- `star` (boolean, optional): Star it.
- `label` (string or enum, optional): Put this label on it, by name.
- `forward` (string or enum, optional): Forward it to this address. Asks for a passkey.
- `delete` (boolean, optional): Move it to Trash.
- `neverJunk` (boolean, optional): Never send it to Junk.
- `important` (boolean, optional): Mark it important.
- `neverImportant` (boolean, optional): Never mark it important. Beats `important`.
- `tab` (enum, optional): File it in this inbox tab. One of `primary`, `updates`, `newsletters`, `promotions`, ``.

### Returns

`data` is an object with:

- `matched` (number)
- `sample` (array of [Conversation](https://mailset.app/docs/api/threads#conversation) objects): The newest few, to check by eye.
- `capped` (boolean): The count stopped at the scan limit, so there may be more.

### Request

TypeScript:

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

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

const answer = await mailset.rules.test({
	mailbox: "ana@example.com",
	from: "stripe.com",
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.test(
    {"mailbox": "ana@example.com", "from": "stripe.com"}
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesTestParams{
		Mailbox: mailset.String("ana@example.com"),
		From:    mailset.String("stripe.com"),
	}
	answer, err := client.Rules.Test(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.test(
  mailbox: 'ana@example.com',
  from: 'stripe.com'
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->test([
    'mailbox' => 'ana@example.com',
    'from' => 'stripe.com',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.RuleTest;
import app.mailset.model.RulesTestParams;

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

var params = RulesTestParams.builder()
        .mailbox("ana@example.com")
        .from("stripe.com")
        .build();
RuleTest answer = mailset.rules().test(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesTestParams;

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

let answer = mailset
    .rules()
    .test(RulesTestParams {
        mailbox: Some("ana@example.com".into()),
        from: Some("stripe.com".into()),
        ..Default::default()
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.test \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"mailbox":"ana@example.com","from":"stripe.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 rules test --mailbox ana@example.com --from stripe.com
```

### Response

```json
{
  "ok": true,
  "data": {
    "matched": 1,
    "sample": [
      {
        "id": "out_3f9a1c0e7b2d",
        "mailbox": "text",
        "from": "hello@example.com",
        "fromAddress": "hello@example.com",
        "subject": "Your Acme receipt",
        "snippet": "text",
        "count": 12,
        "latestAt": 1796048940000,
        "unread": true,
        "starred": true,
        "important": true,
        "place": "inbox",
        "sent": true,
        "hasAttachment": true,
        "labels": [
          "text"
        ],
        "snoozedUntil": 1796048940000,
        "tab": "primary",
        "needsReply": true,
        "followUp": true,
        "picture": {
          "url": "https://hooks.example.com/mailset",
          "icon": true
        },
        "senderDeactivated": true
      }
    ],
    "capped": true
  }
}
```

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

## rules.apply

Run a rule over the conversations already here, in the background.

`POST /v1/rules.apply`

Any signed-in person, with an API token.

### Parameters

- `id` (string, required): The rule’s id, from `rules list`.

### Returns

`data` is an object with:

- `id` (string): The run’s id.
- `state` (enum): `running`, `done` or `failed`. One of `running`, `done`, `failed`.
- `scanned` (number): Conversations looked at so far.
- `changed` (number): Of those, how many the rule changed.
- `reason` (nullable string)

### Request

TypeScript:

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

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

const answer = await mailset.rules.apply({
	id: "rl_1c7e3a9f5b2d4068e4a9c2f1",
});
console.log(answer);
```

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.apply(
    {"id": "rl_1c7e3a9f5b2d4068e4a9c2f1"}
)
print(answer)
```

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesApplyParams{
		ID: "rl_1c7e3a9f5b2d4068e4a9c2f1",
	}
	answer, err := client.Rules.Apply(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.apply(
  id: 'rl_1c7e3a9f5b2d4068e4a9c2f1'
)
p answer
```

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->apply([
    'id' => 'rl_1c7e3a9f5b2d4068e4a9c2f1',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.RuleRun;
import app.mailset.model.RulesApplyParams;

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

var params = RulesApplyParams.builder()
        .id("rl_1c7e3a9f5b2d4068e4a9c2f1")
        .build();
RuleRun answer = mailset.rules().apply(params);
System.out.println(answer);
```

Rust:

```rust
use mailset::types::RulesApplyParams;

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

let answer = mailset
    .rules()
    .apply(RulesApplyParams {
        id: "rl_1c7e3a9f5b2d4068e4a9c2f1".into(),
    })
    .await?;
println!("{answer:?}");
```

curl:

```bash
curl https://mailset.app/v1/rules.apply \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"id":"rl_1c7e3a9f5b2d4068e4a9c2f1"}}'
```

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 rules apply rl_1c7e3a9f5b2d4068e4a9c2f1
```

### Response

```json
{
  "ok": true,
  "data": {
    "id": "out_3f9a1c0e7b2d",
    "state": "running",
    "scanned": 1,
    "changed": 1,
    "reason": "The day’s sending limit is reached."
  }
}
```

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

## rules.import

Turn a Gmail mailFilters.xml export into rules.

`POST /v1/rules.import`

Any signed-in person, with an API token. It can stop for a passkey approval.

### Parameters

- `mailbox` (string, required): The mailbox the rules go on.
- `name` (string, required): The file’s name. At most 255 characters.
- `data` (string, required): The file, base64.

Beside `input`:

- `approval` (string, optional): An approval’s id, once someone approved it with their passkey. Without one, the answer is `step_up_required` and names what to approve.

### Returns

`data` is an object with:

- `imported` (number)
- `skipped` (number)
- `items` (array of objects)
  - `filter` (string): The filter in the file’s own words: `from:a subject:b`.
  - `rule` (nullable string): Null when it wasn’t imported.
  - `notes` (array of strings): Why it, or a part of it, was left out.

### Request

TypeScript:

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

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

const answer = await mailset.rules.import({
	mailbox: "ana@example.com",
	name: "mailFilters.xml",
	data: "<the message as base64>",
});
console.log(answer);
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

Python:

```python
from mailset import Mailset

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

answer = mailset.rules.import_rules(
    {
        "mailbox": "ana@example.com",
        "name": "mailFilters.xml",
        "data": "<the message as base64>",
    }
)
print(answer)
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

Go:

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

func main() {
	// Reads MAILSET_TOKEN from the environment.
	client, err := mailset.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	params := &mailset.RulesImportParams{
		Mailbox: "ana@example.com",
		Name:    "mailFilters.xml",
		Data:    "<the message as base64>",
	}
	answer, err := client.Rules.Import(ctx, params)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer)
}
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

Ruby:

```ruby
require 'mailset'

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

answer = mailset.rules.import(
  mailbox: 'ana@example.com',
  name: 'mailFilters.xml',
  data: '<the message as base64>'
)
p answer
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

PHP:

```php
<?php

require 'vendor/autoload.php';

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

$answer = $mailset->rules->import([
    'mailbox' => 'ana@example.com',
    'name' => 'mailFilters.xml',
    'data' => '<the message as base64>',
]);
echo json_encode($answer, JSON_PRETTY_PRINT), PHP_EOL;
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

Java:

```java
import app.mailset.Mailset;
import app.mailset.model.RuleImportReport;
import app.mailset.model.RulesImportParams;

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

var params = RulesImportParams.builder()
        .mailbox("ana@example.com")
        .name("mailFilters.xml")
        .data("<the message as base64>")
        .build();
RuleImportReport answer =
        mailset.rules().importRules(params);
System.out.println(answer);
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

Rust:

```rust
use mailset::types::RulesImportParams;

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

let answer = mailset
    .rules()
    .import(RulesImportParams {
        mailbox: "ana@example.com".into(),
        name: "mailFilters.xml".into(),
        data: "<the message as base64>".into(),
    })
    .await?;
println!("{answer:?}");
```

`<the message as base64>` is a whole email, headers and body: [Send your own MIME](https://mailset.app/docs/recipes/mime).

curl:

```bash
curl https://mailset.app/v1/rules.import \
  -H "Authorization: Bearer $TOKEN" \
  -H "ms-time: $MS_TIME" \
  -H "ms-proof: $MS_PROOF" \
  -H "Content-Type: application/json" \
  -d '{"input":{"mailbox":"ana@example.com","name":"mailFilters.xml","data":"<base64 of the file>"}}'
```

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 rules import ana@example.com --file mailFilters.xml
```

### Response

```json
{
  "ok": true,
  "data": {
    "imported": 1,
    "skipped": 1,
    "items": [
      {
        "filter": "text",
        "rule": "text",
        "notes": [
          "text"
        ]
      }
    ]
  }
}
```

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

---

© 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