---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://docs.sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://docs.sendcorex.com/llms.txt"
---

Source: https://docs.sendcorex.com/pl/api-reference/receive-emails/audience/save-segment

Odbieranie e-maili: kontakty i odbiorcy

# Save Segment

Tworzy lub aktualizuje segment tej skrzynki.

POST `https://g.sendcorex.com/v1` command: save_segment

Segment należy do jednej skrzynki. Aby użyć go w innej, zapisz tam kopię.

Wymaga uprawnienia do organizowania (niedostępne dla tokenów tylko do odczytu).

## Argumenty

- `segment_id` string opcjonalne
Pomiń, by utworzyć nowy segment.
- `name` string wymagane
Do 200 znaków.
- `where` object wymagane
Filtr. Ten sam format co `list_contacts`.
- `sort` object opcjonalne
`{ field }`: `email`, `created_at`, `last_open_at`, `last_click_at`, `open_count` lub `click_count`.

## Szybki start

```
import axios from 'axios';

const response = await axios.post(
  'https://g.sendcorex.com/v1',
  {
    commands: [
      {
        command: "save_segment",
        args: {
          name: "Opened in 30 days",
          where: {
            last_open_at: {
              within_days: 30
            }
          }
        }
      }
    ]
  },
  { headers: { Authorization: process.env.SENDCOREX_TOKEN } },
);

const [result] = response.data.results;
if (result.type !== 'SUCCESS') throw new Error(`${result.code}: ${result.message}`);
console.log(result.data);
```

## Odpowiedź

```
{
  "type": "SUCCESS",
  "results": [
    {
      "command": "save_segment",
      "id": "0",
      "type": "SUCCESS",
      "data": {
        "segment": {
          "segment_id": "seg_8Hq2",
          "name": "Opened in 30 days",
          "where": {
            "last_open_at": {
              "within_days": 30
            }
          },
          "sort": null,
          "created_at": "2026-11-21T10:00:00Z",
          "updated_at": "2026-11-21T10:00:00Z"
        }
      }
    }
  ]
}
```

### Pola odpowiedzi

- `segment`
Zapisany segment.

## Błędy

Błędy na poziomie HTTP zwracają `type`, `code` i `message`. Błędy oznaczone CMD wracają w wyniku polecenia, z HTTP 200.

- 401`TOKEN_EXPIRED`
Access token wygasł. Wywołaj Refresh Token i powtórz zapytanie.
- 401`INVALID_TOKEN`
Token jest uszkodzony, nieznany lub unieważniony.
- 429`RATE_LIMITED`
Ponad 15 zapytań/s lub 15 jednocześnie dla tego samego IP + skrzynki. Odczekaj liczbę sekund z Retry-After.
- CMD`INVALID_ARGS`
Brakuje wymaganego argumentu lub jest nieprawidłowy.
- CMD`NOT_FOUND`
Wiadomość, wątek lub folder nie istnieje dla tego tokena.
- CMD`READ_ONLY_ACCESS`
Token lub aplikacja ma dostęp Tylko odczyt, a polecenie zapisuje. W trasie przesyłania załączników to HTTP 403.
- CMD`INTERNAL`
Błąd serwera. Szczegóły trafiają tylko do logu serwera.
