> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailglyph.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Overview

> Install and initialize the MailGlyph Python SDK.

Use the official Python SDK to call the MailGlyph API from server-side apps.

* PyPI package: [mailglyph](https://pypi.org/project/mailglyph/)
* GitHub repository: [MailGlyph Python SDK](https://github.com/MailGlyph/mailglyph-python)

## Prerequisites

Before you start, make sure you have:

* A MailGlyph **secret API key** (`sk_...`) for server-side API requests
* A MailGlyph **public API key** (`pk_...`) for event tracking (`/v1/track`)
* At least one **verified sending domain** for email sending

Setup links:

* [MailGlyph API keys dashboard](https://app.mailglyph.com/api-keys)
* [MailGlyph domains dashboard](https://app.mailglyph.com/domains)
* [API keys guide](https://docs.mailglyph.com/guides/api-keys.md)
* [Domain verification guide](https://docs.mailglyph.com/guides/verifying-domains.md)

## Install

```bash theme={null}
pip install mailglyph
```

## Initialize clients

Create one SDK client for your secret key and one for your public key:

```python theme={null}
from mailglyph import AsyncMailGlyph, MailGlyph

secret_client = MailGlyph("sk_your_secret_key")
public_client = MailGlyph("pk_your_public_key")

async_secret_client = AsyncMailGlyph("sk_your_secret_key")
async_public_client = AsyncMailGlyph("pk_your_public_key")
```

Set environment variables:

```bash theme={null}
export MAILGLYPH_SECRET_KEY="sk_your_secret_key"
export MAILGLYPH_PUBLIC_KEY="pk_your_public_key"
```

## Configure base URL, timeout, and retries

Use this when testing against staging or local environments:

```python theme={null}
from mailglyph import MailGlyph

client = MailGlyph(
    "sk_your_secret_key",
    base_url="https://api.mailglyph.com",
    timeout=30.0,
    max_retries=3,
)
```

## Key usage rules

The Python SDK enforces key restrictions automatically:

* `pk_*` keys can only call `events.track`
* `sk_*` keys can call all non-track endpoints, including `events.get_names`

```python theme={null}
from mailglyph import MailGlyph
from mailglyph.exceptions import AuthenticationError

secret_client = MailGlyph("sk_your_secret_key")

try:
    secret_client.events.track(email="user@example.com", event="purchase")
except AuthenticationError as error:
    print(error.message)
finally:
    secret_client.close()
```

## Close clients

Use context managers or call `close()` / `await close()` explicitly:

```python theme={null}
from mailglyph import AsyncMailGlyph, MailGlyph

with MailGlyph("sk_your_secret_key") as client:
    verification = client.emails.verify("user@example.com")
    print(verification.valid)


async def run_async() -> None:
    async with AsyncMailGlyph("sk_your_secret_key") as client:
        verification = await client.emails.verify("user@example.com")
        print(verification.valid)
```

## Next pages

* [Emails examples](https://docs.mailglyph.com/sdks/python/emails.md)
* [Email Validation examples](https://docs.mailglyph.com/sdks/python/email-validation.md)
* [Events examples](https://docs.mailglyph.com/sdks/python/events.md)
* [Contacts examples](https://docs.mailglyph.com/sdks/python/contacts.md)
* [Segments examples](https://docs.mailglyph.com/sdks/python/segments.md)
* [Campaigns examples](https://docs.mailglyph.com/sdks/python/campaigns.md)
* [Error handling](https://docs.mailglyph.com/sdks/python/errors.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.