---
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/routes/refresh-token

Odbieranie e-maili: trasy

# Refresh Token

Wymień refresh token na nowy access token i nowy refresh token. Stara para przestaje działać natychmiast.

POST `https://g.sendcorex.com/v1/token/refresh`

Ta trasa nie wymaga nagłówka `Authorization`. Access token jest ważny 1 godzinę, a refresh token 15 dni.

Refresh token jest jednorazowy. Zapisz nowe `token` i `refresh_token`, zanim zrobisz cokolwiek innego, bo stara para przestaje działać, gdy tylko to wywołanie się powiedzie.

## Parametry body

- `refresh_token` string wymagane
Obecny refresh token (`R.S.C.G.…`).

## Szybki start

```
import axios from 'axios';

const response = await axios.post(
  'https://g.sendcorex.com/v1/token/refresh',
  {
    refresh_token: "R.S.C.G.xxxxxxxx"
  },
);

console.log(response.data);
```

## Odpowiedź

```
{
  "type": "SUCCESS",
  "data": {
    "token": "S.C.G.xxxxxxxx",
    "refresh_token": "R.S.C.G.xxxxxxxx",
    "expires_at": "2026-10-10T09:33:00Z",
    "refresh_expires_at": "2026-10-25T08:33:00Z"
  }
}
```

### Pola odpowiedzi

- `data.token`
Nowy access token. Wysyłaj go w nagłówku `Authorization`.
- `data.refresh_token`
Nowy refresh token. Użyj go raz, przy następnym odświeżeniu.
- `data.expires_at`
Kiedy wygasa access token (UTC).
- `data.refresh_expires_at`
Kiedy wygasa refresh token (UTC).

## Odświeżaj automatycznie

Odświeżaj, gdy zapytanie zakończy się `TOKEN_EXPIRED`, i dbaj, by naraz działało tylko jedno odświeżenie: refresh token można użyć raz.

Node.js

```
import axios from 'axios';

const api = axios.create({
  baseURL: 'https://g.sendcorex.com',
  validateStatus: () => true, // handle 4xx/5xx ourselves
});

let token = process.env.SENDCOREX_TOKEN;
let refreshToken = process.env.SENDCOREX_REFRESH_TOKEN;

async function refresh() {
  const res = await api.post('/v1/token/refresh', { refresh_token: refreshToken });
  if (res.status !== 200) throw new Error('refresh failed: ' + res.data?.code);
  token = res.data.data.token;
  refreshToken = res.data.data.refresh_token;
  // Persist both values (database / secret store): the old refresh token is dead.
  return res.data.data;
}

// One refresh at a time, because the refresh token is single-use.
let refreshing = null;

export async function callWithRefresh(commands) {
  const send = () => api.post('/v1', { commands }, { headers: { Authorization: token } });

  let res = await send();
  if (res.status === 401 && res.data?.code === 'TOKEN_EXPIRED') {
    refreshing ??= refresh().finally(() => (refreshing = null));
    await refreshing;
    res = await send();
  }
  if (res.status !== 200) throw new Error(res.data?.code ?? 'HTTP ' + res.status);
  return res.data.results;
}
```

Jeśli odświeżenie zakończy się `INVALID_REFRESH_TOKEN`, pary nie da się odzyskać. Wygeneruj nowy token w panelu.

## Błędy

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

- 401`INVALID_REFRESH_TOKEN`
Refresh token jest nieprawidłowy, wygasł, został unieważniony lub już użyty. Wygeneruj nowy token w panelu.
- 429`RATE_LIMITED`
Ponad 15 zapytań/s lub 15 jednocześnie dla tego samego IP + skrzynki. Odczekaj liczbę sekund z Retry-After.
- 503`AUTH_UNAVAILABLE`
Uwierzytelnianie jest chwilowo niedostępne. Spróbuj ponownie.
