Skip to main content

Python client for the Nellie AI Book Generation API

Project description

Nellie Python SDK

The official Python client for the Nellie AI Book Generation API.

SDK Version: 0.1.0
API Version: v1

Installation

pip install nellie-api

Usage

Basic Book Generation

from nellie_api import Nellie

client = Nellie(api_key="nel_...")

# Start a book generation job
book = client.books.create(
    prompt="A sci-fi mystery about a lost colony on Mars",
    style="sci_fi",
    type="novel"
)

print(f"Book started! ID: {book.request_id}")

# Check status
status = client.books.retrieve(book.request_id)
print(f"Status: {status.status} ({status.progress}%)")

if status.status == "completed":
    print(f"Download URL: {status.result_url}")

Waiting for Completion

Use the built-in polling helper to wait for a job to finish:

from nellie_api import Nellie

client = Nellie(api_key="nel_...")

book = client.books.create(prompt="A fantasy adventure")

# Block until complete (with progress callback)
result = client.books.wait_for_completion(
    book.request_id,
    poll_interval=120,  # Check every 2 minutes (minimum: 60s)
    timeout=7200,       # Max wait time: 2 hours
    on_progress=lambda s: print(f"Progress: {s.progress}%")
)

if result.is_successful():
    print(f"Done! Download: {result.result_url}")
else:
    print(f"Failed: {result.error}")

Getting Configuration Options

Fetch available styles, types, and output formats:

config = client.get_configuration()
print(f"Available styles: {config.styles}")
print(f"Available types: {config.types}")
print(f"Available formats: {config.formats}")

Getting Available Models

Retrieve available generation models and their costs:

models = client.get_models()
for model in models:
    print(f"{model.name}: {model.cost_per_book} credits")

Checking Usage Statistics

Get your credit consumption and request history:

usage = client.get_usage()
print(f"Total requests: {usage.total_requests}")
print(f"Total credits used: {usage.total_credits_used}")

for req in usage.recent_requests:
    print(f"  {req.request_id}: {req.status} ({req.credits_used} credits)")

Type Hints

The SDK exports type literals for better IDE support:

from nellie_api import BookStyle, BookType, ModelVersion, OutputFormat

style: BookStyle = "sci_fi"
type: BookType = "novel"

Configuration

You can configure the client using environment variables or constructor arguments.

Environment Variables:

  • NELLIE_API_KEY: Your API key.
  • NELLIE_API_BASE_URL: (Optional) Override the API base URL.

Constructor:

client = Nellie(
    api_key="nel_...", 
    base_url="https://api.nelliewriter.com",  # SDK automatically uses /v1 paths
    max_retries=3,  # Configures automatic retries for rate limits
    timeout=30      # Request timeout in seconds (default: 30)
)

Webhooks

Verify incoming webhooks securely using the built-in utility:

from nellie_api import Webhook, WebhookSignatureError
import os

# Your webhook secret from the dashboard
webhook_secret = os.environ.get("NELLIE_WEBHOOK_SECRET")

# In your Flask/Django/FastAPI route:
def webhook_handler(request):
    payload = request.data
    sig_header = request.headers.get("X-Nellie-Signature")

    try:
        event = Webhook.construct_event(
            payload, sig_header, webhook_secret
        )
    except WebhookSignatureError as e:
        return "Invalid signature", 400

    # Handle the event
    if event.status == "completed":
        print(f"Book ready: {event.result_url}")
    
    return "Success", 200

Error Handling

The SDK raises exceptions for various error conditions:

  • AuthenticationError: Invalid API key (HTTP 401).
  • RateLimitError: Too many requests (HTTP 429).
  • APIError: General API errors (e.g. invalid parameters, server errors).
  • NellieError: Base class for all SDK errors.
from nellie_api import Nellie, APIError, AuthenticationError, RateLimitError

try:
    client.books.create(...)
except AuthenticationError:
    print("Check your API key")
except RateLimitError:
    print("Rate limit exceeded - wait before retrying")
except APIError as e:
    print(f"API Error: {e}")

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

nellie_api-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

nellie_api-0.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file nellie_api-0.1.0.tar.gz.

File metadata

  • Download URL: nellie_api-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for nellie_api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d832c23b958e4040e4495b96bf3c63c3f1a8f60de551e20c161698f47c919677
MD5 d40b9f9eea5c5ef4d8eb6738e70462cd
BLAKE2b-256 94b7d83453ecfe23e7c01fcb4c363f3912cd2867601ad68403f845f9c86daa16

See more details on using hashes here.

File details

Details for the file nellie_api-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nellie_api-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for nellie_api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33d299027a8dc39fcede597017fcad44722f9351653b99a9078322f2b8d0b3cd
MD5 8a223f4448a644a75eba565f0c82431e
BLAKE2b-256 8453b33b9ef095aea756c80bb83fa0ff51f36ec200611cb8c053601de5eeaad5

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