---
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/vi/api-reference/receive-emails/routes/refresh-token

Nhận email: tuyến

# Refresh Token

Đổi refresh token lấy access token mới và refresh token mới. Cặp cũ ngừng hoạt động ngay.

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

Tuyến này không cần header `Authorization`. Access token có hiệu lực 1 giờ và refresh token 15 ngày.

Refresh token chỉ dùng một lần. Lưu `token` và `refresh_token` mới trước khi làm gì khác, vì cặp cũ vô hiệu ngay khi lần gọi này thành công.

## Tham số body

- `refresh_token` string bắt buộc
Refresh token hiện tại (`R.S.C.G.…`).

## Bắt đầu nhanh

```
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);
```

## Phản hồi

```
{
  "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"
  }
}
```

### Trường phản hồi

- `data.token`
Access token mới. Gửi nó trong header `Authorization`.
- `data.refresh_token`
Refresh token mới. Dùng một lần, ở lần làm mới tiếp theo.
- `data.expires_at`
Khi access token hết hạn (UTC).
- `data.refresh_expires_at`
Khi refresh token hết hạn (UTC).

## Tự động làm mới

Làm mới khi yêu cầu thất bại với `TOKEN_EXPIRED`, và đảm bảo mỗi lúc chỉ có một lần làm mới: refresh token chỉ dùng được một lần.

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;
}
```

Nếu làm mới thất bại với `INVALID_REFRESH_TOKEN`, cặp token không thể khôi phục. Tạo token mới trong dashboard.

## Lỗi

Lỗi ở mức HTTP trả về `type`, `code` và `message`. Lỗi đánh dấu CMD được trả về trong kết quả lệnh, với HTTP 200.

- 401`INVALID_REFRESH_TOKEN`
Refresh token không hợp lệ, hết hạn, bị thu hồi hoặc đã dùng. Tạo token mới trong dashboard.
- 429`RATE_LIMITED`
Hơn 15 yêu cầu/giây hoặc 15 yêu cầu đồng thời cho cùng IP + hộp thư. Chờ số giây trong Retry-After.
- 503`AUTH_UNAVAILABLE`
Xác thực tạm thời không khả dụng. Hãy thử lại.
