# SendGrid Integration via LowCodeAPI

## Overview

SendGrid is a leading transactional email service for sending emails at scale. The SendGrid API provides comprehensive functionality for:

- **Email Sending** - Send single and bulk emails
- **Mail Send** - Advanced email customization and personalization
- **Contacts** - Manage recipient contacts
- **Lists** - Organize contacts into lists
- **Campaigns** - Create and manage email campaigns
- **Templates** - Use email templates for consistency
- **Statistics** - Track email performance metrics

## Base Endpoint

```
https://api.lowcodeapi.com/sendgrid/
```

## Authentication

LowCodeAPI handles authentication automatically using Bearer token authentication. You only need to:

1. **Sign up** at [SendGrid](https://sendgrid.com) to get your API Key
2. **Connect your account** in the LowCodeAPI dashboard
3. **Use your `api_token`** in all requests

The `api_token` is your LowCodeAPI authentication token. LowCodeAPI will automatically:
- Fetch your SendGrid API key
- Apply it to each request as a Bearer token

**Auth Type**: Bearer Token

## API Categories

- **Transactional Email** - Email delivery and management

## Common Endpoints

### Category: Mail Send

#### Send Email

**Method**: `POST` | **LowCodeAPI Path**: `/v3/mail/send`

**Full URL**:
```
https://api.lowcodeapi.com/sendgrid/v3/mail/send?api_token={api_token}
```

**Description**: Send a single email or personalized batch of emails.

**Request Body**:
```json
{
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]",
          "name": "Recipient Name"
        }
      ],
      "cc": [
        {
          "email": "[email protected]"
        }
      ],
      "subject": "Hello from SendGrid"
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Sender Name"
  },
  "reply_to": {
    "email": "[email protected]"
  },
  "subject": "Hello from SendGrid",
  "content": [
    {
      "type": "text/html",
      "value": "<html><body><h1>Hello!</h1><p>This is a test email.</p></body></html>"
    }
  ]
}
```

**Request Body Fields**:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `personalizations` | array | Yes | Array of recipient personalizations |
| `from` | object | Yes | Sender email address and name |
| `reply_to` | object | No | Reply-to address |
| `subject` | string | Yes | Email subject line |
| `content` | array | Yes | Email content (HTML and/or text) |
| `attachments` | array | No | Email attachments |
| `categories` | array | No | Categories for tracking |

**Personalization Fields**:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `to` | array | Yes | Recipient email addresses |
| `cc` | array | No | CC recipients |
| `bcc` | array | No | BCC recipients |
| `subject` | string | No | Subject line for this personalization |
| `substitutions` | object | No | Substitution variables |

**Example Request**:
```bash
curl -X POST "https://api.lowcodeapi.com/sendgrid/v3/mail/send?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "personalizations": [
      {
        "to": [{"email": "[email protected]", "name": "Recipient Name"}],
        "subject": "Hello from SendGrid"
      }
    ],
    "from": {"email": "[email protected]", "name": "Sender Name"},
    "content": [
      {
        "type": "text/html",
        "value": "<html><body><h1>Hello!</h1><p>This is a test email.</p></body></html>"
      }
    ]
  }'
```

**Official Documentation**: [Send Mail](https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html)

---

#### Send Batch Email

**Method**: `POST` | **LowCodeAPI Path**: `/v3/mail/send`

**Full URL**:
```
https://api.lowcodeapi.com/sendgrid/v3/mail/send?api_token={api_token}
```

**Description**: Send personalized emails to multiple recipients.

**Request Body**:
```json
{
  "personalizations": [
    {
      "to": [{"email": "[email protected]"}],
      "substitutions": {
        "%name%": "User One"
      }
    },
    {
      "to": [{"email": "[email protected]"}],
      "substitutions": {
        "%name%": "User Two"
      }
    }
  ],
  "from": {"email": "[email protected]"},
  "subject": "Hello %name%",
  "content": [
    {
      "type": "text/html",
      "value": "<h1>Hello %name%</h1><p>This is your personalized email.</p>"
    }
  ]
}
```

**Example Request**:
```bash
curl -X POST "https://api.lowcodeapi.com/sendgrid/v3/mail/send?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "personalizations": [
      {
        "to": [{"email": "[email protected]"}],
        "substitutions": {"%name%": "User One"}
      },
      {
        "to": [{"email": "[email protected]"}],
        "substitutions": {"%name%": "User Two"}
      }
    ],
    "from": {"email": "[email protected]"},
    "subject": "Hello %name%",
    "content": [
      {
        "type": "text/html",
        "value": "<h1>Hello %name%</h1><p>This is your personalized email.</p>"
      }
    ]
  }'
```

**Official Documentation**: [Batch Sending](https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html)

---

### Category: Contacts

#### Get All Contacts

**Method**: `GET` | **LowCodeAPI Path**: `/v3/marketing/contacts`

**Full URL**:
```
https://api.lowcodeapi.com/sendgrid/v3/marketing/contacts?api_token={api_token}
```

**Description**: Retrieve all contacts with pagination.

**Query Parameters**:

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `page_size` | integer | No | Number of results per page |
| `page_token` | string | No | Token for pagination |

**Example Request**:
```bash
curl -X GET "https://api.lowcodeapi.com/sendgrid/v3/marketing/contacts?page_size=100&api_token=YOUR_API_TOKEN"
```

**Official Documentation**: [Get Contacts](https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html)

---

#### Add Contact

**Method**: `PUT` | **LowCodeAPI Path**: `/v3/marketing/contacts`

**Full URL**:
```
https://api.lowcodeapi.com/sendgrid/v3/marketing/contacts?api_token={api_token}
```

**Description**: Add a new contact or update existing contact.

**Request Body**:
```json
{
  "contacts": [
    {
      "email": "[email protected]",
      "first_name": "John",
      "last_name": "Doe",
      "custom_fields": {
        "company": "Acme Inc"
      }
    }
  ]
}
```

**Request Body Fields**:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `contacts` | array | Yes | Array of contact objects |
| `list_ids` | array | No | List IDs to add contacts to |
| `custom_fields` | object | No | Custom field values |

**Example Request**:
```bash
curl -X PUT "https://api.lowcodeapi.com/sendgrid/v3/marketing/contacts?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "contacts": [
      {
        "email": "[email protected]",
        "first_name": "John",
        "last_name": "Doe"
      }
    ]
  }'
```

**Official Documentation**: [Add Contact](https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html)

---

### Category: Lists

#### Get All Lists

**Method**: `GET` | **LowCodeAPI Path**: `/v3/marketing/lists`

**Full URL**:
```
https://api.lowcodeapi.com/sendgrid/v3/marketing/lists?api_token={api_token}
```

**Description**: Retrieve all contact lists.

**Example Request**:
```bash
curl -X GET "https://api.lowcodeapi.com/sendgrid/v3/marketing/lists?api_token=YOUR_API_TOKEN"
```

**Official Documentation**: [Get Lists](https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html)

---

### Category: Templates

#### Get Templates

**Method**: `GET` | **LowCodeAPI Path**: `/v3/templates`

**Full URL**:
```
https://api.lowcodeapi.com/sendgrid/v3/templates?api_token={api_token}
```

**Description**: Retrieve all email templates.

**Query Parameters**:

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `generations` | string | No | Filter by template generation (legacy, dynamic) |

**Example Request**:
```bash
curl -X GET "https://api.lowcodeapi.com/sendgrid/v3/templates?generations=dynamic&api_token=YOUR_API_TOKEN"
```

**Official Documentation**: [Get Templates](https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html)

---

## Usage Examples

### Example 1: Send Simple Email

Send a basic HTML email:

```bash
# Send a simple email
# No ID needed - sends email
curl -X POST "https://api.lowcodeapi.com/sendgrid/v3/mail/send?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "personalizations": [
      {
        "to": [{"email": "[email protected]"}],
        "subject": "Welcome to Our Service"
      }
    ],
    "from": {"email": "[email protected]", "name": "My App"},
    "content": [
      {
        "type": "text/html",
        "value": "<html><body><h1>Welcome!</h1><p>Thank you for signing up.</p></body></html>"
      }
    ]
  }'
```

### Example 2: Send Personalized Bulk Email

Send personalized emails to multiple recipients:

```bash
# Send personalized batch emails
# No ID needed - sends to all recipients
curl -X POST "https://api.lowcodeapi.com/sendgrid/v3/mail/send?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "personalizations": [
      {
        "to": [{"email": "[email protected]", "name": "User One"}],
        "subject": "Special Offer for You"
      },
      {
        "to": [{"email": "[email protected]", "name": "User Two"}],
        "subject": "Special Offer for You"
      }
    ],
    "from": {"email": "[email protected]"},
    "content": [
      {
        "type": "text/html",
        "value": "<html><body><h1>Exclusive Offer!</h1><p>Get 20% off today.</p></body></html>"
      }
    ]
  }'
```

### Example 3: Manage Contacts

Add and retrieve contacts:

```bash
# Step 1: Add new contacts
# No ID needed - creates contacts
curl -X PUT "https://api.lowcodeapi.com/sendgrid/v3/marketing/contacts?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "contacts": [
      {"email": "[email protected]", "first_name": "Jane", "last_name": "Smith"},
      {"email": "[email protected]", "first_name": "Bob", "last_name": "Jones"}
    ]
  }'

# Step 2: Get all contacts
# No ID needed - lists all contacts
curl -X GET "https://api.lowcodeapi.com/sendgrid/v3/marketing/contacts?page_size=100&api_token=YOUR_API_TOKEN"

# Step 3: Get all lists
# No ID needed - lists all contact lists
curl -X GET "https://api.lowcodeapi.com/sendgrid/v3/marketing/lists?api_token=YOUR_API_TOKEN"
```

## Complete Endpoint Reference

For a complete list of all 351 endpoints and their parameters, refer to:

- **OpenAPI Definition**: https://backend.lowcodeapi.com/sendgrid/definition
- **Official SendGrid Documentation**: https://sendgrid.com/docs/API_Reference

## Rate Limits & Best Practices

- **Rate Limit**: Refer to your SendGrid plan for rate limits
- **Best Practices**:
  - Use personalizations for batch sends instead of multiple API calls
  - Implement proper error handling and retry logic
  - Use categories to track email campaigns
  - Validate email addresses before sending
  - Use templates for consistent email formatting
  - Monitor bounce and complaint rates
  - Use custom_args for tracking metadata
  - Implement exponential backoff for rate limiting

## Error Handling

All responses are wrapped in a `data` key:
```json
{
  "data": {
    // Actual response from SendGrid
  }
}
```

Common errors:
- **400**: Invalid request format or parameters
- **401**: Invalid API key
- **413**: Payload too large (max attachment size: 40MB total)
- **429**: Rate limit exceeded
- **500**: SendGrid server error