# Email to Ticket – Postman Collection Examples

**Collection file:** `docs/Email_To_Ticket_API.postman_collection.json`  
**Base URL variable:** `{{web_url}}` = `https://your-api-host/api/v1/web`

**Collection variables to set:**

| Variable | Description |
|----------|-------------|
| `web_url` | Base URL, e.g. `https://sys.roms.roqay.dev/api/v1/web` |
| `auth_token` | Bearer token (for setup endpoints 2–7) |
| `support_email_id` | ID of support email (for update/delete/toggle) |
| `email_to_ticket_secret` | Same as `EMAIL_TO_TICKET_PROCESS_SECRET` in `.env`. Example: `roms-email-to-ticket-8f3a2b1c4d5e6f7a9b0c1d2e3f4a5b6c` (for Process Incoming only) |

**Auth summary:**
- **Process Incoming (1):** Use header `X-Email-To-Ticket-Secret: {{email_to_ticket_secret}}` only. Do **not** send `Authorization: Bearer ...` (this request uses "No Auth" in Postman).
- **All others (2–7):** Use `Authorization: Bearer {{auth_token}}` (collection default).

---

## 1. Process Incoming Email (Listener)

**Auth:** No Bearer. Use header: `X-Email-To-Ticket-Secret: {{email_to_ticket_secret}}`

```
POST {{web_url}}/email-to-ticket/process-incoming
Accept: application/json
Content-Type: application/json
X-Email-To-Ticket-Secret: {{email_to_ticket_secret}}
```

**Body (raw JSON):**
```json
{
  "to": "support@example.com",
  "from": "client@example.com",
  "from_name": "Client Name",
  "subject": "Issue with login",
  "body": "I cannot log in to the portal. Please help.",
  "attachments": [],
  "message_id": null,
  "references": null,
  "in_reply_to": null
}
```

**Reply example (add as comment to existing ticket):** Use same body; ensure subject or `references`/`in_reply_to` allow resolving ticket (e.g. subject contains `#ST-123` or `[Ticket #123]`).

---

## 2. GET Page Data

**Auth:** Bearer `{{auth_token}}`

```
GET {{web_url}}/email-to-ticket
Accept: application/json
Authorization: Bearer {{auth_token}}
```

No body.

---

## 3. PUT Save Settings

**Auth:** Bearer `{{auth_token}}`

```
PUT {{web_url}}/email-to-ticket/settings
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{auth_token}}
```

**Body:**
```json
{
  "default_ticket_type": 1,
  "default_priority": 3
}
```
`default_ticket_type`: 1 = Issue, 2 = New Feature. `default_priority`: 1 = Critical, 2 = High, 3 = Medium, 4 = Low.

---

## 4. POST Add Email

**Auth:** Bearer `{{auth_token}}`

```
POST {{web_url}}/email-to-ticket/emails
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{auth_token}}
```

**Body:**
```json
{
  "email": "support@example.com",
  "label": "Main Support"
}
```

---

## 5. PUT Update Email

**Auth:** Bearer `{{auth_token}}`  
**URL:** Replace `{id}` with `{{support_email_id}}`.

```
PUT {{web_url}}/email-to-ticket/emails/{{support_email_id}}
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{auth_token}}
```

**Body (label only):**
```json
{
  "label": "Primary Support"
}
```

**Body (email + label, only when `email_editable` is true):**
```json
{
  "email": "updated-support@example.com",
  "label": "Primary Support"
}
```

---

## 6. DELETE Remove Email

**Auth:** Bearer `{{auth_token}}`

```
DELETE {{web_url}}/email-to-ticket/emails/{{support_email_id}}
Accept: application/json
Authorization: Bearer {{auth_token}}
```

No body.

---

## 7. POST Toggle Email Status

**Auth:** Bearer `{{auth_token}}`

```
POST {{web_url}}/email-to-ticket/emails/{{support_email_id}}/toggle-status
Accept: application/json
Authorization: Bearer {{auth_token}}
```

No body.
