Skip to main content

Official Python SDK for the Jasni AI Email API

Project description

Jasni Python SDK

Official Python SDK for the Jasni AI Email API. Build AI-powered email automation, agents, and integrations.

Installation

pip install jasni-sdk

Quick Start

from jasni import Jasni

# Initialize the client with your API key
jasni = Jasni("jsk_your_api_key")

# List your email accounts
accounts = jasni.accounts.list()
print(f"You have {accounts.total} accounts")

# Send an email
result = jasni.emails.send(
    from_="me@mail.jasni.ai",
    to="recipient@example.com",
    subject="Hello from Jasni!",
    text="This email was sent using the Jasni Python SDK.",
)
print(f"Email sent with ID: {result.message_id}")

Async Support

The SDK also supports async operations:

import asyncio
from jasni import AsyncJasni

async def main():
    jasni = AsyncJasni("jsk_your_api_key")
    
    # List emails
    emails = await jasni.emails.list(account="me@mail.jasni.ai")
    for email in emails.emails:
        print(f"- {email.subject}")

asyncio.run(main())

Features

Email Accounts

# List all accounts
accounts = jasni.accounts.list()

# Create a new account
new_account = jasni.accounts.create(
    username="myaccount",
    name="My Account"
)

# Delete an account
jasni.accounts.delete("myaccount@mail.jasni.ai")

Emails

# List emails from an account
emails = jasni.emails.list(
    account="me@mail.jasni.ai",
    folder="INBOX",
    limit=20
)

# Get a specific email
email = jasni.emails.get(
    uid=123,
    account="me@mail.jasni.ai"
)

# Send an email
jasni.emails.send(
    from_="me@mail.jasni.ai",
    to=["recipient1@example.com", "recipient2@example.com"],
    subject="Hello!",
    text="Plain text body",
    html="<p>HTML body</p>",
    cc="cc@example.com",
    bcc="bcc@example.com"
)

# Reply to an email
jasni.emails.reply(
    uid=123,
    account="me@mail.jasni.ai",
    text="Thanks for your email!",
    reply_all=False
)

# Forward an email
jasni.emails.forward(
    uid=123,
    account="me@mail.jasni.ai",
    to="colleague@example.com",
    text="FYI - see below"
)

# Mark as read/unread
jasni.emails.mark_as_read(123, account="me@mail.jasni.ai")
jasni.emails.mark_as_unread(123, account="me@mail.jasni.ai")

# Delete an email
jasni.emails.delete(123, account="me@mail.jasni.ai")

Email Labels

# Get labels for an email
labels = jasni.emails.labels.list(
    uid=123,
    account="me@mail.jasni.ai"
)

# Assign labels to an email
jasni.emails.labels.assign(
    uid=123,
    account="me@mail.jasni.ai",
    labels=["Important", "Work"],
    agent_name="my-classifier"
)

# Remove labels from an email
jasni.emails.labels.remove(
    uid=123,
    account="me@mail.jasni.ai",
    label_ids=["label-id-1", "label-id-2"]
)

Drafts

# List drafts
drafts = jasni.drafts.list(account="me@mail.jasni.ai")

# Create a draft
draft = jasni.drafts.create(
    account="me@mail.jasni.ai",
    to="recipient@example.com",
    subject="Draft subject",
    text="Draft body"
)

# Update a draft
jasni.drafts.update(
    account="me@mail.jasni.ai",
    uid=123,
    subject="Updated subject",
    text="Updated body"
)

# Delete a draft
jasni.drafts.delete(account="me@mail.jasni.ai", uid=123)

Labels

# List all labels
labels = jasni.labels.list(include_count=True)

# Create a label
label = jasni.labels.create(
    name="Important",
    color="#ff0000",
    description="Important emails"
)

Webhooks

# List webhooks
webhooks = jasni.webhooks.list()

# Create a webhook
webhook = jasni.webhooks.create(
    url="https://my-server.com/webhook",
    events=["email.received", "email.sent"],
    description="My email webhook"
)
# Save the secret! It's only returned on creation
print(f"Webhook secret: {webhook.webhook.secret}")

# Update a webhook
jasni.webhooks.update(
    id="webhook-id",
    url="https://new-url.com/webhook",
    active=False
)

# Delete a webhook
jasni.webhooks.delete("webhook-id")

Error Handling

The SDK raises specific exceptions for different error cases:

from jasni import Jasni
from jasni.errors import (
    JasniError,
    AuthenticationError,
    NotFoundError,
    ValidationError,
    RateLimitError,
    ConflictError,
    ServerError,
)

jasni = Jasni("jsk_your_api_key")

try:
    email = jasni.emails.get(uid=999, account="me@mail.jasni.ai")
except AuthenticationError:
    print("Invalid API key")
except NotFoundError:
    print("Email not found")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")
except ValidationError as e:
    print(f"Invalid request: {e}")
except ServerError:
    print("Server error, please try again later")
except JasniError as e:
    print(f"API error: {e}")

Configuration

from jasni import Jasni

# Custom base URL (for self-hosted or testing)
jasni = Jasni(
    "jsk_your_api_key",
    base_url="https://custom-api.jasni.ai"
)

# With timeout configuration
jasni = Jasni(
    "jsk_your_api_key",
    timeout=30.0  # seconds
)

Webhook Signature Verification

Verify webhook signatures to ensure requests are from Jasni:

from jasni.webhooks import verify_signature

# In your webhook handler
def handle_webhook(request):
    payload = request.body
    signature = request.headers.get("X-Jasni-Signature")
    
    if verify_signature(payload, signature, webhook_secret):
        # Process the webhook
        data = json.loads(payload)
        print(f"Received event: {data['event']}")
    else:
        return "Invalid signature", 401

Requirements

  • Python 3.8+
  • httpx
  • pydantic

License

MIT License - see LICENSE for details.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jasni_sdk-0.1.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jasni_sdk-0.1.1-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file jasni_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: jasni_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for jasni_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f3227f1938caf3c258326169bde8a8f0682d488860a011fd542fc40303c848aa
MD5 87cfb59e47fd35e7ee247a69746f02f9
BLAKE2b-256 a7d05d86efab55d9d1a6568267f96610f085c688aa6e74fb3f994eabcf66740f

See more details on using hashes here.

File details

Details for the file jasni_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: jasni_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for jasni_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8bd812ed4c5aeb261beb47771d40ea61e60370b2f3771f47f02e6f919de3dd5
MD5 fbef0b78a7e2bf12b8a917b7be8892c3
BLAKE2b-256 13b988aab91da71ee0a9e6f190aaf41aa868c8a79dd466925dbb142671a00c09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page