Skip to content

Repository files navigation

DhivehiGPT TypeScript SDK

Tests Coverage npm version License: MIT

A lightweight, dependency-free TypeScript/JavaScript SDK for the DhivehiGPT API — voices, text-to-speech, subscription balance, and task pricing.

Works in any modern Node.js server (18+) and in any JavaScript runtime with a global fetch. Ships as both ESM and CommonJS with full TypeScript types, and has zero runtime dependencies.

Installation

npm install @javaabu/dhivehigpt-sdk

You'll need an API key, generated from the API Keys page of your team account. This requires a DhivehiGPT team account with an active subscription, and only team owners and admins can generate API keys.

Quick start

import { DhivehiGPT } from '@javaabu/dhivehigpt-sdk';

// Reads the API key from the DHIVEHIGPT_API_KEY environment variable automatically.
const client = new DhivehiGPT();

// List available voices
const { data: voices } = await client.voices.list({ gender: 'female' });

// Generate a TTS audio
const audio = await client.tts.generate({
  text: 'ދިވެހިޖީޕީޓީއަށް މަރުޙަބާ',
  voice: 'hajja',
});

console.log(audio.audio_url);

CommonJS works the same way:

const { DhivehiGPT } = require('@javaabu/dhivehigpt-sdk');

const client = new DhivehiGPT(); // also reads DHIVEHIGPT_API_KEY

Usage

The client exposes one namespace per API resource:

client.voices.list(params?);        // GET /v1/voices
client.voices.get(slug);            // GET /v1/voices/{slug}

client.tts.list(params?);             // GET /v1/tts
client.tts.generate({ text, voice }); // POST /v1/tts
client.tts.get(uuid);                 // GET /v1/tts/{uuid}
client.tts.update(uuid, { title });   // PUT /v1/tts/{uuid}
client.tts.delete(uuid);              // DELETE /v1/tts/{uuid}

client.subscriptions.getBalance();  // GET /v1/balance

client.tasks.list(params?);              // GET /v1/tasks
client.tasks.get(slug);                  // GET /v1/tasks/{slug}
client.tasks.calculate({ task, units }); // POST /v1/tasks/calculate

Error handling

Every non-2xx response throws a typed error you can catch:

import { AuthenticationError, InsufficientCreditsError, ValidationError } from '@javaabu/dhivehigpt-sdk';

try {
  await client.tts.generate({ text: '...', voice: 'hajja' });
} catch (error) {
  if (error instanceof InsufficientCreditsError) {
    // team is out of credits
  } else if (error instanceof AuthenticationError) {
    // missing, invalid, or revoked API key
  } else if (error instanceof ValidationError) {
    console.log(error.errors); // field-level validation errors
  } else {
    throw error;
  }
}

Client options

new DhivehiGPT({
  apiKey: '...',            // optional if DHIVEHIGPT_API_KEY is set in the environment
  baseUrl: '...',           // defaults to https://api.dhivehigpt.com
  apiVersion: 'v1',         // defaults to "v1"
  timeoutMs: 10_000,        // optional request timeout
  headers: { 'X-Foo': 1 },  // optional extra headers on every request
  fetch: myFetch,           // optional custom fetch implementation
});

Full documentation

The full documentation, including every resource, type, and error, is published at docs.javaabu.com/docs/dhivehigpt-sdk-typescript.

See also the examples directory for runnable sample scripts.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving this package? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email info@javaabu.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

DhivehiGPT SDK to interact with the DhivehiGPT API using Typescript and Javascript server-side applications

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages