|
| 1 | +# DhivehiGPT SDK for Python |
| 2 | + |
| 3 | +[](https://pypi.org/project/dhivehigpt-sdk/) |
| 4 | +[](../../actions/workflows/run-tests.yml) |
| 5 | + |
| 6 | +[](https://pypi.org/project/dhivehigpt-sdk/) |
| 7 | + |
| 8 | +A lightweight, zero-dependency Python SDK for the [DhivehiGPT API](https://dhivehigpt.com/docs). |
| 9 | + |
| 10 | +## Requirements |
| 11 | + |
| 12 | +- Python 3.8 or higher |
| 13 | +- No runtime dependencies — the SDK only uses the Python standard library. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +pip install dhivehigpt-sdk |
| 19 | +``` |
| 20 | + |
| 21 | +## Getting an API key |
| 22 | + |
| 23 | +API keys are generated from your DhivehiGPT team's [API Keys](https://dhivehigpt.com/team/api-keys) page. You'll need: |
| 24 | + |
| 25 | +- A DhivehiGPT team account with an **active subscription**. |
| 26 | +- To be a **team owner** or **team admin** — only owners and admins can generate API keys. |
| 27 | + |
| 28 | +## Quick start |
| 29 | + |
| 30 | +The client reads the API key from the `DHIVEHIGPT_API_KEY` environment variable by default: |
| 31 | + |
| 32 | +```bash |
| 33 | +export DHIVEHIGPT_API_KEY="your-api-key" |
| 34 | +``` |
| 35 | + |
| 36 | +```python |
| 37 | +from dhivehigpt_sdk import DhivehiGPT |
| 38 | + |
| 39 | +client = DhivehiGPT() |
| 40 | + |
| 41 | +# or pass the API key explicitly |
| 42 | +client = DhivehiGPT(api_key="your-api-key") |
| 43 | +``` |
| 44 | + |
| 45 | +`base_url` and `api_version` are optional and default to `https://api.dhivehigpt.com` and `v1`: |
| 46 | + |
| 47 | +```python |
| 48 | +client = DhivehiGPT( |
| 49 | + api_key="your-api-key", |
| 50 | + base_url="https://api.dhivehigpt.com", |
| 51 | + api_version="v1", |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +### Voices |
| 58 | + |
| 59 | +```python |
| 60 | +voices = client.voices.list(gender="female", is_premium=False) |
| 61 | +voice = client.voices.get("hajja") |
| 62 | +``` |
| 63 | + |
| 64 | +### Text-to-Speech (TTS) |
| 65 | + |
| 66 | +```python |
| 67 | +audio = client.tts.generate(text="ދިވެހިޖީޕީޓީއަށް މަރުޙަބާ", voice="hajja") |
| 68 | + |
| 69 | +audios = client.tts.list(voice="hajja", per_page=20) |
| 70 | +one = client.tts.get(audio["data"]["uuid"]) |
| 71 | +client.tts.update(audio["data"]["uuid"], title="Welcome message") |
| 72 | +client.tts.delete(audio["data"]["uuid"]) |
| 73 | +``` |
| 74 | + |
| 75 | +### Balance |
| 76 | + |
| 77 | +```python |
| 78 | +balance = client.balance.get() |
| 79 | +print(balance["billing_period"]["balance"]) |
| 80 | +``` |
| 81 | + |
| 82 | +### Tasks |
| 83 | + |
| 84 | +```python |
| 85 | +tasks = client.tasks.list(platform="tts") |
| 86 | +task = client.tasks.get("audio_generation") |
| 87 | +cost = client.tasks.calculate(task="audio_generation", units=1000) |
| 88 | +``` |
| 89 | + |
| 90 | +## Error handling |
| 91 | + |
| 92 | +All errors raised by the SDK inherit from `dhivehigpt_sdk.DhivehiGPTError`: |
| 93 | + |
| 94 | +```python |
| 95 | +from dhivehigpt_sdk import DhivehiGPT, DhivehiGPTError, NotFoundError |
| 96 | + |
| 97 | +client = DhivehiGPT() |
| 98 | + |
| 99 | +try: |
| 100 | + client.voices.get("does-not-exist") |
| 101 | +except NotFoundError as e: |
| 102 | + print(e.status_code, e.message) |
| 103 | +except DhivehiGPTError as e: |
| 104 | + print("Something else went wrong:", e) |
| 105 | +``` |
| 106 | + |
| 107 | +| Exception | Cause | |
| 108 | +|--------------------------|-----------------------------------------------------| |
| 109 | +| `AuthenticationError` | Missing, invalid, or revoked API key (401) | |
| 110 | +| `PermissionDeniedError` | Not enough credits, etc. (403) | |
| 111 | +| `NotFoundError` | Resource does not exist (404) | |
| 112 | +| `ValidationError` | Invalid request data (422); see `error.errors` | |
| 113 | +| `RateLimitError` | Exceeded 60 requests/minute per API key (429) | |
| 114 | +| `ServerError` | Upstream error (5xx) | |
| 115 | +| `APIConnectionError` | Could not reach the API | |
| 116 | + |
| 117 | +## Documentation |
| 118 | + |
| 119 | +Full documentation is available at [https://docs.javaabu.com/docs/dhivehigpt-sdk-python](https://docs.javaabu.com/docs/dhivehigpt-sdk-python). |
| 120 | + |
| 121 | +The full DhivehiGPT API reference is available at [https://dhivehigpt.com/docs](https://dhivehigpt.com/docs). |
| 122 | + |
| 123 | +Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions? Feel free to create an [issue](../../issues) on GitHub, we'll try to address it as soon as possible. |
| 124 | + |
| 125 | +If you've found a bug regarding security please mail [info@javaabu.com](mailto:info@javaabu.com) instead of using the issue tracker. |
| 126 | + |
| 127 | +## Testing |
| 128 | + |
| 129 | +```bash |
| 130 | +pip install -e ".[test]" |
| 131 | +pytest |
| 132 | +``` |
| 133 | + |
| 134 | +## Changelog |
| 135 | + |
| 136 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 137 | + |
| 138 | +## Contributing |
| 139 | + |
| 140 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 141 | + |
| 142 | +## Security |
| 143 | + |
| 144 | +If you discover any security related issues, please email [info@javaabu.com](mailto:info@javaabu.com) instead of using the issue tracker. |
| 145 | + |
| 146 | +## Credits |
| 147 | + |
| 148 | +- [Javaabu Pvt. Ltd.](https://github.com/javaabu) |
| 149 | +- [All Contributors](../../contributors) |
| 150 | + |
| 151 | +## License |
| 152 | + |
| 153 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments