---
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/audience/save-segment

รับอีเมล: ผู้ติดต่อและกลุ่มเป้าหมาย

# Save Segment

สร้างหรืออัปเดตกลุ่มของกล่องจดหมายนี้

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

กลุ่มเป็นของกล่องจดหมายเดียว หากจะใช้ในกล่องอื่น ให้บันทึกสำเนาไว้ที่นั่น

ต้องมีสิทธิ์จัดระเบียบ (โทเค็นอ่านอย่างเดียวใช้ไม่ได้)

## อาร์กิวเมนต์

- `segment_id` string ไม่บังคับ
ไม่ใส่เพื่อสร้างกลุ่มใหม่
- `name` string จำเป็น
สูงสุด 200 ตัวอักษร
- `where` object จำเป็น
ตัวกรอง รูปแบบเดียวกับ `list_contacts`
- `sort` object ไม่บังคับ
`{ field }`: `email`, `created_at`, `last_open_at`, `last_click_at`, `open_count` หรือ `click_count`

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

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

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

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

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

- `segment`
กลุ่มที่บันทึกแล้ว

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

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

- 401`TOKEN_EXPIRED`
access token หมดอายุแล้ว เรียก Refresh Token แล้วส่งคำขอซ้ำ
- 401`INVALID_TOKEN`
โทเค็นผิดรูปแบบ ไม่รู้จัก หรือถูกเพิกถอน
- 429`RATE_LIMITED`
เกิน 15 คำขอ/วินาที หรือ 15 คำขอพร้อมกันสำหรับ IP + กล่องจดหมายเดียวกัน รอจำนวนวินาทีใน Retry-After
- CMD`INVALID_ARGS`
อาร์กิวเมนต์ที่จำเป็นหายไปหรือไม่ถูกต้อง
- CMD`NOT_FOUND`
ไม่มีข้อความ เธรด หรือโฟลเดอร์นี้สำหรับโทเค็นนี้
- CMD`READ_ONLY_ACCESS`
โทเค็นหรือแอปมีสิทธิ์อ่านอย่างเดียวและคำสั่งมีการเขียน ในเส้นทางอัปโหลดไฟล์แนบคือ HTTP 403
- CMD`INTERNAL`
ข้อผิดพลาดของเซิร์ฟเวอร์ รายละเอียดเขียนเฉพาะในล็อกเซิร์ฟเวอร์
