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

รับอีเมล: เส้นทาง

# Refresh Token

แลก refresh token เป็น access token ใหม่และ refresh token ใหม่ คู่เดิมใช้ไม่ได้ทันที

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

เส้นทางนี้ไม่ต้องใช้เฮดเดอร์ `Authorization` access token ใช้ได้ 1 ชั่วโมง และ refresh token ใช้ได้ 15 วัน

refresh token ใช้ได้ครั้งเดียว บันทึก `token` และ `refresh_token` ใหม่ก่อนทำอย่างอื่น เพราะคู่เดิมใช้ไม่ได้ทันทีที่การเรียกนี้สำเร็จ

## พารามิเตอร์ body

- `refresh_token` string จำเป็น
refresh token ปัจจุบัน (`R.S.C.G.…`)

## เริ่มต้นอย่างรวดเร็ว

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

## การตอบกลับ

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

### ฟิลด์การตอบกลับ

- `data.token`
access token ใหม่ ส่งในเฮดเดอร์ `Authorization`
- `data.refresh_token`
refresh token ใหม่ ใช้ครั้งเดียวในการรีเฟรชครั้งถัดไป
- `data.expires_at`
เวลาที่ access token หมดอายุ (UTC)
- `data.refresh_expires_at`
เวลาที่ refresh token หมดอายุ (UTC)

## รีเฟรชอัตโนมัติ

รีเฟรชเมื่อคำขอล้มเหลวด้วย `TOKEN_EXPIRED` และให้มีการรีเฟรชทีละครั้ง: refresh token ใช้ได้ครั้งเดียว

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

หากรีเฟรชล้มเหลวด้วย `INVALID_REFRESH_TOKEN` คู่โทเค็นกู้คืนไม่ได้ สร้างโทเค็นใหม่ในแดชบอร์ด

## ข้อผิดพลาด

ข้อผิดพลาดระดับ HTTP ส่งกลับ `type`, `code` และ `message` ข้อผิดพลาดที่มีเครื่องหมาย CMD จะกลับมาในผลลัพธ์ของคำสั่ง พร้อม HTTP 200

- 401`INVALID_REFRESH_TOKEN`
refresh token ไม่ถูกต้อง หมดอายุ ถูกเพิกถอน หรือใช้ไปแล้ว สร้างโทเค็นใหม่ในแดชบอร์ด
- 429`RATE_LIMITED`
เกิน 15 คำขอ/วินาที หรือ 15 คำขอพร้อมกันสำหรับ IP + กล่องจดหมายเดียวกัน รอจำนวนวินาทีใน Retry-After
- 503`AUTH_UNAVAILABLE`
การยืนยันตัวตนใช้งานไม่ได้ชั่วคราว ลองอีกครั้ง
