Metadata-Version: 2.4
Name: keyid
Version: 0.4.2
Summary: KeyID.ai SDK — agent email infrastructure
License: MIT
Project-URL: Homepage, https://keyid.ai
Project-URL: Repository, https://github.com/KeyID-AI/sdk-py
Keywords: keyid,agent,ai-agent,email,agent-email,ed25519,identity,llm,ai-tools,ai-infrastructure,autonomous
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: httpx>=0.25
Requires-Dist: cryptography>=41.0

# keyid

**Free autonomy for AI agents. One key to email, phone, search, storage, hosting, 2FA, and more.**

One `provision()` call and your agent has full autonomy — real email, phone number, web search, file storage, page hosting, 2FA handling, cron scheduling, and encrypted vault. Zero API keys, zero cost, zero human involvement. The key is all it takes.

[KeyID.ai](https://keyid.ai) handles identity provisioning, domain rotation, reputation monitoring, deliverability, phone pool management, and infrastructure scaling. Your agent just generates a keypair and calls `provision()`.

## Install

```bash
pip install keyid
```

## Quick Start

```python
from keyid import KeyID

agent = KeyID()

# Register — get an email address instantly
result = agent.provision()
print(f"Agent email: {result['email']}")

# Optionally request a long-lived phone number
phone_result = agent.request_phone()
print(f"Agent phone: {phone_result['phone']}")

# Read inbox (email + SMS unified)
inbox = agent.get_inbox()
sms_only = agent.get_inbox(channel="sms")

# Send email
agent.send("user@example.com", "Hello", "Message body")

# Reply to a message
agent.reply(inbox["messages"][0]["id"], "Thanks!")
```

## SMS verification (signing up for a service that sends an OTP)

For one-shot SMS verification on a third-party signup form, lease a fresh phone number for just that flow. The number is exclusive to your session and is released back to the shared pool when you complete it.

```python
import time

# Start a session and reserve a phone for it
session = agent.start_registration_session(
    service_name="example.com",
    expected_channels=["sms"],
    use_sms_lease=True,
)
print(f"Use this phone on the signup form: {session['leasedPhone']}")

# (Submit the form with session['leasedPhone'] — handled by your code)

# Poll until the OTP arrives
while True:
    result = agent.get_registration_artifacts(session["id"])
    if result["artifacts"]:
        break
    time.sleep(2)
otp = next(a["artifactValue"] for a in result["artifacts"] if a["artifactType"] == "otp_code")

# Submit the OTP, then release the lease so the phone returns to the pool
agent.complete_registration_session(session["id"])
```

### Phone numbers: persistent vs leased

|  | `request_phone()` | `start_registration_session(use_sms_lease=True)` |
|---|---|---|
| **Lifetime** | Long-lived, tied to the agent identity | Short-lived, tied to one signup session |
| **Returned via** | `request_phone()` and `get_identity()["phone"]` | `session["leasedPhone"]` |
| **Use case** | Long-running agent that needs a stable inbound number | One-shot SMS verification on a third-party site |
| **Released by** | Agent retirement / `recover()` | `complete_registration_session(id)` or `block_registration_session(id)` |
| **Exclusive?** | No, shared pool of agents per number | **Yes** — only one active leased session per agent |

> **Lease lifecycle:** Leased phones are exclusive per agent. If you start a new session with `use_sms_lease=True` while an old one is still open, the server returns `Phone already leased by another active signup session`. Always call `complete_registration_session` (or `block_registration_session` on failure) to release the lease. Sessions also auto-expire at `session["expiresAt"]`.

> **If you have a `CLAUDE.md`, agent prompt, or memory note that says "KeyID = email", update it.** KeyID provisions phone numbers and handles SMS too — both persistent (`request_phone`) and leased per-signup (`use_sms_lease`).

## Authentication

KeyID uses Ed25519 challenge-response authentication. The SDK handles this automatically:

1. On first use, a keypair is generated (or loaded from env/options)
2. `provision()` registers the public key and returns an email address
3. All subsequent calls auto-authenticate via signed nonce exchange

```python
# Option 1: Auto-generate keypair (default)
agent = KeyID()

# Option 2: Provide existing keypair
agent = KeyID(public_key="...hex...", private_key="...hex...")

# Option 3: Custom base URL
agent = KeyID(base_url="https://your-instance.com")
```

## API Reference

### Identity

| Method | Description |
|--------|-------------|
| `provision()` | Register agent, get email |
| `request_phone()` | Request a phone number (opt-in, authenticated) |
| `get_identity()` | Full profile (email, phone, avatarUrl, bio, reputation score/tier) |
| `get_addresses()` | List all addresses (current + historical) |
| `update_identity(**kwargs)` | Update profile (display_name, avatar_url, bio, website_url, profile_public) |
| `get_reputation()` | Get own reputation score (0-100), tier, factor breakdown |
| `get_public_profile(agent_id)` | Get another agent's public profile (no auth required) |
| `recover(recovery_token, new_public_key?, new_private_key?)` | Rotate keypair using recovery token |

### Messages

| Method | Description |
|--------|-------------|
| `get_inbox(**kwargs)` | Fetch inbox with pagination, filtering, search, channel filter |
| `get_message(id)` | Get single message detail |
| `update_message(id, **kwargs)` | Update labels, read/starred status |
| `get_unread_count()` | Count unread inbound messages |
| `send(to, subject, body, **kwargs)` | Send email (HTML, CC/BCC, scheduled) |
| `reply(message_id, body, **kwargs)` | Reply to a message |
| `reply_all(message_id, body, **kwargs)` | Reply-all |
| `forward(message_id, to, body=None)` | Forward a message |

### Threads

| Method | Description |
|--------|-------------|
| `list_threads(**kwargs)` | List conversation threads |
| `get_thread(thread_id)` | Get thread with all messages |
| `delete_thread(thread_id, permanent=False)` | Delete thread |

### Drafts

| Method | Description |
|--------|-------------|
| `create_draft(**kwargs)` | Create a draft |
| `get_draft(draft_id)` | Get draft detail |
| `update_draft(draft_id, **kwargs)` | Update draft |
| `delete_draft(draft_id)` | Delete draft |
| `send_draft(draft_id)` | Send a draft |

### Settings

| Method | Description |
|--------|-------------|
| `get_signature()` | Get email signature |
| `set_signature(signature)` | Set email signature |
| `get_forwarding()` | Get forwarding settings |
| `set_forwarding(forwarding_address)` | Configure email forwarding |
| `get_auto_reply()` | Get auto-reply/vacation settings |
| `set_auto_reply(**kwargs)` | Configure auto-reply |

### Contacts

| Method | Description |
|--------|-------------|
| `list_contacts(**kwargs)` | List saved contacts |
| `create_contact(**kwargs)` | Create a contact |
| `get_contact(contact_id)` | Get contact detail |
| `update_contact(contact_id, **kwargs)` | Update contact |
| `delete_contact(contact_id)` | Delete contact |

### Webhooks

| Method | Description |
|--------|-------------|
| `list_webhooks()` | List webhooks |
| `create_webhook(url, events=None)` | Create webhook |
| `get_webhook(webhook_id)` | Get webhook detail |
| `update_webhook(webhook_id, **kwargs)` | Update webhook |
| `delete_webhook(webhook_id)` | Delete webhook |
| `get_webhook_deliveries(**kwargs)` | Delivery history |

### Verification

| Method | Description |
|--------|-------------|
| `get_links(message_id)` | Extract links from a message |
| `get_codes(message_id)` | Extract verification codes from a message |
| `follow_link(message_id=None, link_index=None, url=None)` | Follow a verification link, returns final URL and redirects |

### TOTP / 2FA

| Method | Description |
|--------|-------------|
| `register_totp(service_name=..., secret=..., **opts)` or `register_totp(uri="otpauth://...")` | Register a TOTP secret |
| `list_totp()` | List all stored TOTP entries (secrets are never returned) |
| `get_totp_code(totp_id)` | Generate the current 6-digit code + seconds remaining in the window |
| `delete_totp(totp_id)` | Remove a TOTP entry |

### Persona

| Method | Description |
|--------|-------------|
| `get_persona()` | Get agent persona profile |
| `create_persona(**kwargs)` | Create persona profile |
| `update_persona(**kwargs)` | Update persona profile |

### Registration Log

Lightweight log of which services this agent has signed up for.

| Method | Description |
|--------|-------------|
| `add_registration(service_name, **kwargs)` | Log a service registration |
| `list_registrations(**kwargs)` | List registrations with optional filters |
| `get_registration(id)` | Get registration by ID |
| `update_registration(id, **kwargs)` | Update a registration |
| `delete_registration(id)` | Delete a registration |

### Registration Sessions

Active signup flows. A registration session correlates email / SMS / TOTP artifacts (verification codes, magic links, backup codes) for one signup so you can wait for the right one without scanning the whole inbox. Pass `use_sms_lease=True` to reserve a phone number for the flow — see the [SMS verification](#sms-verification-signing-up-for-a-service-that-sends-an-otp) example above.

| Method | Description |
|--------|-------------|
| `start_registration_session(**opts)` | Start a session. Pass `use_sms_lease=True` to reserve a phone |
| `list_registration_sessions(**opts)` | List recent sessions |
| `get_registration_session(id)` | Get one session including current status and any leased phone |
| `get_registration_artifacts(id)` | Pull all extracted artifacts (OTPs, magic links, TOTP URIs, backup codes) |
| `save_browser_state(id, state)` | Persist cookies / localStorage between session steps |
| `load_browser_state(id)` | Restore previously saved browser state |
| `complete_registration_session(id)` | Mark done and release any leased phone |
| `block_registration_session(id, **opts)` | Mark blocked (failed) and release any leased phone |

### Vault

| Method | Description |
|--------|-------------|
| `list_vault()` | List all vault entries (keys + metadata) |
| `get_vault_entry(key)` | Get a vault entry by key |
| `put_vault_entry(key, value, **kwargs)` | Store a value in the vault |
| `delete_vault_entry(key)` | Delete a vault entry |

### Search

| Method | Description |
|--------|-------------|
| `search(query, *, num=None, country=None, time_range=None)` | Search the web via Google |
| `get_search_usage()` | Today's search usage and quota |

### Storage

| Method | Description |
|--------|-------------|
| `store_list(*, prefix=None, page=1, limit=50)` | List stored files |
| `store_get(key)` | Get file metadata |
| `store_download(key)` | Download file content (base64) |
| `store_put(key, content, *, content_type=None, is_public=None)` | Upload or overwrite a file. `content` accepts `str` (UTF-8 text) or `bytes`; the SDK base64-encodes for you. |
| `store_delete(key)` | Delete a file |
| `store_set_public(key, is_public)` | Toggle public URL |
| `get_store_usage()` | Storage usage and quota |

### Scheduling

| Method | Description |
|--------|-------------|
| `list_crons()` | List cron jobs |
| `create_cron(**kwargs)` | Create a cron job |
| `get_cron(cron_id)` | Get cron job detail |
| `update_cron(cron_id, **kwargs)` | Update a cron job |
| `delete_cron(cron_id)` | Delete a cron job |

### Agent Pages

| Method | Description |
|--------|-------------|
| `create_page(slug, **kwargs)` | Create a page with a slug |
| `list_pages()` | List agent's pages |
| `get_page(slug)` | Get page details |
| `update_page(slug, **kwargs)` | Update page title/description/published |
| `delete_page(slug)` | Delete page and all files |
| `upload_page_file(slug, path, content, **kwargs)` | Upload a file to a page. `content` accepts `str` (UTF-8 text) or `bytes`; the SDK base64-encodes for you. |
| `list_page_files(slug)` | List files in a page |
| `delete_page_file(slug, path)` | Delete a file from a page |

### Lists & Metrics

| Method | Description |
|--------|-------------|
| `add_to_list(direction, type, entry)` | Add to allow/blocklist |
| `remove_from_list(direction, type, entry)` | Remove from list |
| `get_list(direction, type)` | Get list entries |
| `get_metrics(**kwargs)` | Query usage metrics |

## Features

- **Scheduled Send** — `agent.send("to@x.com", "Sub", "Body", scheduled_at="2025-01-01T10:00:00Z")`
- **Full-Text Search** — `agent.get_inbox(search="invoice")`
- **Starred Messages** — `agent.update_message(id, is_starred=True)`
- **Auto-Reply** — `agent.set_auto_reply(enabled=True, body="Out of office")`
- **HTML Email** — `agent.send("to@x.com", "Sub", "text", html="<h1>Hello</h1>")`
- **SMS Inbox** — `agent.get_inbox(channel="sms")` — filter by email or SMS
- **SMS Webhooks** — subscribe to `sms.received` events
- **Persistent phone** — `agent.request_phone()` for a long-lived number tied to the agent
- **Leased phone for signups** — `agent.start_registration_session(use_sms_lease=True)` reserves a fresh number per signup, see [SMS verification](#sms-verification-signing-up-for-a-service-that-sends-an-otp)
- **TOTP / 2FA** — `agent.register_totp(service_name=..., secret=...)`, `agent.get_totp_code(id)` for any 2FA-protected service

## Replaces

One `provision()` call replaces signing up for all of these:

| Category | Services |
|----------|----------|
| Email sending | Resend, SendGrid, Mailgun, Postmark, Amazon SES |
| Email accounts | Gmail, Google Workspace, AgentMail, Outlook |
| SMS / Phone | Twilio, Telnyx, Vonage, Plivo |
| Web search | Serper, Tavily, Brave Search, SerpAPI, Google Custom Search |
| File storage | AWS S3, Cloudflare R2, Google Cloud Storage, UploadThing |
| 2FA / TOTP | Google Authenticator, Authy, 1Password |
| Verification | Manual email checking, link clicking, code copying |
| Scheduling | cron-job.org, AWS EventBridge, Inngest |
| Static hosting | Vercel, Netlify, GitHub Pages, CloudFlare Pages |

## VS Code Extension

For a visual inbox experience during development, install **[KeyID Agent Inbox](https://marketplace.visualstudio.com/items?itemName=keyid.keyid-agent-inbox)** — manage agents, monitor inboxes, extract verification codes, and reply to emails directly in VS Code.

## Requirements

- Python 3.9+
- `httpx` (installed automatically)

## License

MIT
