---
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/idempotency

คู่มือ

# Idempotency (ลองใหม่อย่างปลอดภัย)

ลองส่งใหม่ได้ทุกครั้งโดยไม่ส่งอีเมลซ้ำ ใช้ key หนึ่งตัวต่ออีเมลหนึ่งฉบับ และใช้ key เดิมทุกครั้งที่ลองใหม่

- Idempotency-Key
- idempotency_key
- 24 h

## สิ่งที่แก้ได้

คำขอของคุณหมดเวลา อีเมลถูกส่งไปหรือยัง? ลองใหม่ด้วย key เดิมแล้วคุณจะได้ผลลัพธ์เดิม ไม่มีอะไรถูกส่งซ้ำ

## วิธีใช้

ส่ง header `Idempotency-Key: <key ของคุณ>` ใน `POST /v3.0/send`

```
POST /v3.0/send
Authorization: <api_key>
Idempotency-Key: 3f1c9a52-6b1e-4f0e-9c2a-8d7e5b4a1f00
Content-Type: application/json
```

## กฎ

รูปแบบ key

1–255 ตัวอักษร: ตัวอักษร ตัวเลข `_ - : .` ใช้ UUID ได้ดีที่สุด

ใช้ได้ 24 ชั่วโมง

หลัง 24 ชั่วโมง key เดิมจะนับเป็น key ใหม่

หนึ่ง key ต่ออีเมลหนึ่งฉบับ

ใช้ key ซ้ำเฉพาะเมื่อลองเรียกแบบเดียวกันทุกประการอีกครั้งเท่านั้น

key เดิม แต่เรียกต่างกัน

key เดิมแต่ body หรือ args ต่างกัน จะได้ `IDEMPOTENCY_KEY_REUSED` (422 บน `/v3.0/send`)

การเรียกครั้งแรกยังทำงานอยู่

จะได้ `IDEMPOTENCY_IN_PROGRESS` (409 พร้อม `Retry-After: 1` บน `/v3.0/send`) รอ 1 วินาทีแล้วลองใหม่

เก็บเฉพาะผลที่สำเร็จ

ถ้าการเรียกล้มเหลว การลองใหม่ด้วย key เดิมจะทำงานจริงอีกครั้ง

วิธีดูว่าเป็น replay

header `Idempotent-Replayed: true` บน `/v3.0/send` หรือ `idempotent_replayed: true` ใน `data` บน webmail API

## ตัวอย่างโค้ด

ทุกตัวอย่างสร้าง key หนึ่งตัวและใช้ตัวเดิมทุกครั้งที่ลองใหม่ การหมดเวลาจึงไม่ทำให้อีเมลถูกส่งซ้ำ

POST /v3.0/send

```
KEY=$(uuidgen)   # one key per email, reused on every retry

for attempt in 1 2 3 4 5; do
  status=$(curl -s -o response.json -w '%{http_code}' --max-time 15 \
    https://mail.sendcorex.com/v3.0/send \
    -H "Authorization: $SENDCOREX_API_KEY" \
    -H "Idempotency-Key: $KEY" \
    -H 'Content-Type: application/json' \
    -d '{"to":"user@example.com","from":"noreply@yourdomain.com","senderName":"My App","subject":"Your receipt","body":"<p>Thanks for your order.</p>"}')

  case "$status" in
    000|409|429|5*) sleep $((2 ** (attempt - 1))) ;;   # timeout or busy: retry with the SAME key
    *) break ;;
  esac
done

cat response.json
```

## สำหรับ AI agent

ส่ง idempotency key ทุกครั้ง ถ้าการเรียกหมดเวลา ให้ลองใหม่ด้วย key เดิม อีเมลจะไม่ถูกส่งซ้ำ

## ข้อผิดพลาดที่เกี่ยวข้อง

[`INVALID_IDEMPOTENCY_KEY`](https://docs.sendcorex.com/th/errors#invalid_idempotency_key) [`IDEMPOTENCY_KEY_REUSED`](https://docs.sendcorex.com/th/errors#idempotency_key_reused) [`IDEMPOTENCY_IN_PROGRESS`](https://docs.sendcorex.com/th/errors#idempotency_in_progress) [`IDEMPOTENCY_REPLAY_FAILED`](https://docs.sendcorex.com/th/errors#idempotency_replay_failed)
