# Mail Unsubscribe API - TypeScript Examples

This document provides TypeScript usage examples for the Mail Unsubscribe API API using the Repzo SDK.

## Installation

```bash
npm install repzo
```

## Basic Setup

```typescript
import { Service } from "repzo";

// Initialize with your API key
const apiKey = "your-api-key";
const baseURL = "https://api.repzo.me/v1";
```

## API Usage Examples

### Get all unsubscribed emails

Retrieve a list of all unsubscribed email addresses with optional filtering and pagination

```typescript
// GET /mail-unsubscribe

const params = {
  page: 1, // Page number for pagination

  per_page: 1, // Number of items per page

  sort: "example-value", // Sort field

  email: "example-value", // Filter by email address

  company_namespace: null, // Company namespace for filtering

  _id: null, // Filter by ID
};

const result = await fetch(
  `${baseURL}/mail-unsubscribe?${new URLSearchParams(params)}`,
  {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  },
);

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Add email to unsubscribe list

Add an email address to the unsubscribe list

```typescript
// POST /mail-unsubscribe

const requestBody = {
  // Add your request body properties here
  // Based on the API schema
};

const result = await fetch(`${baseURL}/mail-unsubscribe`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(requestBody),
});

const data = await result.json();
console.log(data);
```

---

### Get unsubscribe record by ID

Retrieve a specific unsubscribe record by its ID

```typescript
// GET /mail-unsubscribe/{id}

const params = {
  id: "example-value", // Unsubscribe record ID
};

const result = await fetch(
  `${baseURL}/mail-unsubscribe/{id}?${new URLSearchParams(params)}`,
  {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  },
);

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Remove email from unsubscribe list

Remove an email address from the unsubscribe list (re-subscribe)

```typescript
// DELETE /mail-unsubscribe/{id}

const id = "your-resource-id";
const deletePath = "/mail-unsubscribe/{id}".replace("{id}", id);

const result = await fetch(`${baseURL}${deletePath}`, {
  method: "DELETE",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
});

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Bulk remove emails from unsubscribe list

Remove multiple email addresses from the unsubscribe list

```typescript
// POST /mail-unsubscribe/remove

const requestBody = {
  // Add your request body properties here
  // Based on the API schema
};

const result = await fetch(`${baseURL}/mail-unsubscribe/remove`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(requestBody),
});

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Check if email is unsubscribed

Check if an email address is in the unsubscribe list

```typescript
// POST /mail-unsubscribe/check

const requestBody = {
  // Add your request body properties here
  // Based on the API schema
};

const result = await fetch(`${baseURL}/mail-unsubscribe/check`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(requestBody),
});

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

## Using with Repzo SDK Service Types

The Repzo SDK provides TypeScript interfaces for type safety:

```typescript
import { Service } from "repzo";

// Example using SDK types

async function example() {
  // Use the Service.Mailunsubscribe types for better type safety
  // This provides intellisense and compile-time type checking
}
```

## Error Handling

```typescript
try {
  const result = await fetch(`${baseURL}/endpoint`, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  });

  if (!result.ok) {
    throw new Error(`HTTP error! status: ${result.status}`);
  }

  const data = await result.json();

  if (!data.success) {
    throw new Error(data.error || "API request failed");
  }

  return data.data;
} catch (error) {
  console.error("API request failed:", error);
  throw error;
}
```

## Pagination

Many API endpoints support pagination:

```typescript
const params = {
  page: 1,
  per_page: 25,
  // other filters...
};

const result = await fetch(
  `${baseURL}/endpoint?${new URLSearchParams(params)}`,
  {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  },
);

const data = await result.json();

// Access pagination info
console.log("Total items:", data.paging.total);
console.log("Current page:", data.paging.page);
console.log("Total pages:", data.paging.pages);
```
