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.

Metadata

Release files for sonexlabs 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sonexlabs 0.1.0
File Size Uploaded Provenance
sonexlabs-0.1.0.tar.gz 8.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sonexlabs 0.1.0
File Interpreter ABI Platform
sonexlabs-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / sonexlabs-0.1.0.tar.gz

Download URL sonexlabs-0.1.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6864303a661c1ec8f80a6de0cd404ab6f16d4f660dae9b5d3c1da31427cd13b5
BLAKE2b-256 checksum
How to use checksums
a6e6c97cf3e7e71440503c4146b23c27208a8ae0614ef908faff4b38d1ed6e5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / sonexlabs-0.1.0-py3-none-any.whl

Download URL sonexlabs-0.1.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1613d90145ae26a6ac48c5380a9f21b671627750364604e94d32adad7386ba2f
BLAKE2b-256 checksum
How to use checksums
6efd850aad5b66b16746315b897b65e9a406d525fea48108bbea49725a6abf6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for sonexlabs 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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