digitalocean/gradient-typescript

TypeScript

Open original ↗

Captured source

source ↗
published Sep 12, 2025seen 5dcaptured 15hhttp 200method plain

digitalocean/gradient-typescript

Description: DigitalOcean Gradient AI Platform SDK

Language: TypeScript

License: Apache-2.0

Stars: 10

Forks: 18

Open issues: 6

Created: 2025-09-12T14:56:10Z

Pushed: 2026-05-27T16:00:58Z

Default branch: main

Fork: no

Archived: no

README:

Gradient TypeScript API Library

This library provides convenient access to the Gradient REST API from server-side TypeScript or JavaScript.

The REST API documentation can be found on developers.digitalocean.com. The full API of this library can be found in [api.md](api.md).

It is generated with Stainless.

Installation

npm install @digitalocean/gradient

Usage

The full API of this library can be found in [api.md](api.md).

import Gradient from '@digitalocean/gradient';

const client = new Gradient({
modelAccessKey: process.env['GRADIENT_MODEL_ACCESS_KEY'], // This is the default and can be omitted
});

const completion = await client.chat.completions.create({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'llama3.3-70b-instruct',
});

console.log(completion.choices);

Streaming responses

We provide support for streaming responses using Server Sent Events (SSE).

import Gradient from '@digitalocean/gradient';

const client = new Gradient();

const stream = await client.chat.completions.create({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'llama3.3-70b-instruct',
stream: true,
});
for await (const chatCompletionChunk of stream) {
console.log(chatCompletionChunk.choices);
}

If you need to cancel a stream, you can break from the loop or call stream.controller.abort().

Request & Response types

This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:

import Gradient from '@digitalocean/gradient';

const client = new Gradient({
modelAccessKey: process.env['GRADIENT_MODEL_ACCESS_KEY'], // This is the default and can be omitted
});

const params: Gradient.Chat.CompletionCreateParams = {
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'llama3.3-70b-instruct',
};
const completion: Gradient.Chat.CompletionCreateResponse = await client.chat.completions.create(params);

Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.

Handling errors

When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of APIError will be thrown:

const completion = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'llama3.3-70b-instruct',
})
.catch(async (err) => {
if (err instanceof Gradient.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});

Error codes are as follows:

| Status Code | Error Type | | ----------- | -------------------------- | | 400 | BadRequestError | | 401 | AuthenticationError | | 403 | PermissionDeniedError | | 404 | NotFoundError | | 422 | UnprocessableEntityError | | 429 | RateLimitError | | >=500 | InternalServerError | | N/A | APIConnectionError |

Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.

You can use the maxRetries option to configure or disable this:

// Configure the default for all requests:
const client = new Gradient({
maxRetries: 0, // default is 2
});

// Or, configure per-request:
await client.chat.completions.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'llama3.3-70b-instruct' }, {
maxRetries: 5,
});

Timeouts

Requests time out after 1 minute by default. You can configure this with a timeout option:

// Configure the default for all requests:
const client = new Gradient({
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
});

// Override per-request:
await client.chat.completions.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'llama3.3-70b-instruct' }, {
timeout: 5 * 1000,
});

On timeout, an APIConnectionTimeoutError is thrown.

Note that requests which time out will be [retried twice by default](#retries).

Advanced Usage

Accessing raw Response data (e.g., headers)

The "raw" Response returned by fetch() can be accessed through the .asResponse() method on the APIPromise type that all methods return. This method returns as soon as the headers for a successful response are received and does not consume the response body, so you are free to write custom parsing or streaming logic.

You can also use the .withResponse() method to get the raw Response along with the parsed data. Unlike .asResponse() this method consumes the body, returning once it is parsed.

const client = new Gradient();

const response = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'llama3.3-70b-instruct',
})
.asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText); // access the underlying Response object

const { data: completion, response: raw } = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'llama3.3-70b-instruct',
})
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(completion.choices);

Logging

> [!IMPORTANT] > All log messages are intended for debugging only. The format and content of log messages > may change between releases.

Log levels

The log level can be configured in two ways:

1. Via the GRADIENT_LOG environment variable 2. Using the logLevel client option (overrides the environment variable if set)

import Gradient from '@digitalocean/gradient';

const client = new Gradient({
logLevel: 'debug', // Show all log messages
});

Available log levels, from most to least verbose:

-…

Excerpt shown — open the source for the full document.

Notability

notability 3.0/10

Low-star routine repo by known company