Skip to main content

Sonex Python SDK

Official Python SDK for SonexLabs.

Requirements

  • Python 3.9+
  • SonexLabs API key

Installation

pip install sonexlabs

Quick Start

from sonex import Sonex

client = Sonex(api_key="YOUR_API_KEY")

Keep your API key in an environment variable rather than hard-coding it in source code.

Text-to-Speech

Only text is required. voice_id, language, speed, and output_format are optional.

audio = client.speech.create(
    text="Hello from Sonex",
)

with open("output.wav", "wb") as f:
    f.write(audio)

language defaults to "auto", speed defaults to 1.0, and output_format defaults to "wav".

Supported output formats:

  • wav
  • mp3
  • ogg

Using a Specific Voice

Get available voices first:

voices = client.voices.list()

for voice in voices:
    print(voice.id, voice.name)

Then use the returned voice ID:

audio = client.speech.create(
    text="Hello from Sonex",
    voice_id="VOICE_ID",
)

with open("output.wav", "wb") as f:
    f.write(audio)

Streaming

Stream audio as it is generated:

with open("output.wav", "wb") as f:
    for chunk in client.speech.stream(
        text="Hello from Sonex",
    ):
        f.write(chunk)

Streaming audio is always returned as chunked WAV. output_format is not accepted for streaming.

Voices

List Voices

voices = client.voices.list()

for voice in voices:
    print(voice.id, voice.name)

Get a Voice

voice = client.voices.get("VOICE_ID")

print(voice.name)
print(voice.language)

Clone a Voice

Clone from a local audio file:

voice = client.voices.clone(
    name="My Custom Voice",
    file="reference.wav",
)

print(voice.id)

Or provide a hosted audio URL:

voice = client.voices.clone(
    name="My Custom Voice",
    audio_url="https://example.com/reference.wav",
)

print(voice.id)

Provide exactly one of file or audio_url.

The reference audio should contain at least 10 seconds of clean audio with one speaker.

Delete a Voice

client.voices.delete("VOICE_ID")

Billing

Check the current account balance:

balance = client.billing.balance()

print(balance.wallet.balance)
print(balance.wallet.currency)

for credit in balance.credits:
    print(credit.service, credit.available)

Error Handling

The SDK provides specific exceptions for common API errors:

from sonex import Sonex
from sonex.exceptions import (
    AuthenticationError,
    InsufficientBalanceError,
    RateLimitError,
    ValidationError,
)

client = Sonex(api_key="YOUR_API_KEY")

try:
    audio = client.speech.create(
        text="Hello from Sonex",
    )
except AuthenticationError:
    print("Invalid API key")
except InsufficientBalanceError:
    print("Insufficient balance")
except RateLimitError:
    print("Rate limit exceeded")
except ValidationError:
    print("Invalid request")

Context Manager

The client can be used as a context manager:

from sonex import Sonex

with Sonex(api_key="YOUR_API_KEY") as client:
    audio = client.speech.create(
        text="Hello from Sonex",
    )

    with open("output.wav", "wb") as f:
        f.write(audio)

Development

Install the package in editable mode:

python -m pip install -e .

Install the test dependency:

python -m pip install pytest

Run the test suite:

python -m pytest

Build the package:

python -m build

The distribution files are generated in the dist/ directory.

API Documentation

For the complete SonexLabs API documentation, see:

License

Proprietary - SonexLabs.

Download files

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

Source Distribution

sonexlabs-0.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

sonexlabs-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sonexlabs-0.1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.10

File hashes

Hashes for sonexlabs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6864303a661c1ec8f80a6de0cd404ab6f16d4f660dae9b5d3c1da31427cd13b5
MD5 f7c7d136384843b20924f8f810d17d4f
BLAKE2b-256 a6e6c97cf3e7e71440503c4146b23c27208a8ae0614ef908faff4b38d1ed6e5d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sonexlabs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.10

File hashes

Hashes for sonexlabs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1613d90145ae26a6ac48c5380a9f21b671627750364604e94d32adad7386ba2f
MD5 818b30729d8b844a25087ce3fa104c58
BLAKE2b-256 6efd850aad5b66b16746315b897b65e9a406d525fea48108bbea49725a6abf6c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page