by Astra

A fully typed TypeScript/JavaScript client library for the Together AI REST API, supporting chat completions, streaming responses, automatic retries, and configurable logging for server-side applications.
This block provides the full Together AI TypeScript SDK source, enabling server-side TypeScript or JavaScript applications to call Together's REST API for chat completions, embeddings, image generation, audio, fine-tuning, and more. It is intended for backend engineers and AI application developers who need type-safe access to Together AI's hosted model infrastructure without relying on the published npm package directly.
source/client.ts - Main Together client class and ClientOptions typesource/index.ts - Top-level re-exports for the entire SDKsource/resource.ts - Base resource class all API resources extendsource/resources.ts - Aggregate export of all resource namespacessource/error.ts - Re-exports all error classessource/streaming.ts - Streaming response utilitiessource/uploads.ts - File upload helperssource/version.ts - SDK version stringsource/api-promise.ts - APIPromise wrapper classsource/core/ - Core abstractions: error types, promise wrapper, streaming, uploadssource/internal/ - Internal utilities: headers, parsing, platform detection, base64, env, logging, UUIDsource/lib/ - Higher-level helpers: chat completion runners, streaming runners, JSON schema, function callingsource/resources/ - All API resource implementations (chat, completions, embeddings, images, audio, fine-tuning, files, models, endpoints, evals, batches, rerank, videos, code interpreter, beta)source/resources/audio/ - Audio sub-resources: speech, transcriptions, translations, voicessource/resources/beta/ - Beta sub-resources: clusters, storage, jig deploymentssource/resources/chat/ - Chat completions resourcesource/resources/models/ - Models listing resourcesource/resources/code-interpreter/ - Code execution resourcenpm install together-ai
The SDK has no additional runtime peer dependencies. It ships with its own internal fetch/stream abstractions that work in Node.js 18+ (native fetch) and modern runtimes. No native build steps, pod installs, or prebuild commands are required.
Spin up an isolated sandbox and run it server-side — no local setup.
Tetrees AI Review of this version
This TypeScript cli / script completed archive review with strong static results. Structure, dependency manifests, documentation, functional source, and common risk patterns were checked by the Tetrees verification pipeline; runtime phases are stated separately.
Deterministic AVCP artifact review
Pipeline avcp-2026-08-04.1 · SHA-256 4cc2e4809ca6c39a…
This version-scoped review deterministically inspects the submitted archive for structure, dependencies, documentation, functional source, and common malicious or high-risk signals. Build and test phases are reported as passed only after an isolated sandbox audition. It is not a guarantee of perfect security.
Reviewed Aug 4, 2026
Push this product straight into your AI IDE, web builder or cloud IDE.
Connect Tetrees to a compatible AI IDE, list products you own, and request the verified ZIP without exposing seller upload controls.
No reviews yet.
Sign in to join the discussion
Loading discussion…
Copy the source/ directory into your project, for example at src/together/.
Add path aliases in tsconfig.json if you want clean imports:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"together/*": ["src/together/*"]
},
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2020",
"strict": true
}
}
Set the required environment variable before running your application:
export TOGETHER_API_KEY="your-api-key-here"
Import directly from the source entry point:
import Together from './src/together/index';
Ensure Node.js 18 or later is used so that the native fetch global is available. For earlier Node versions, install and configure a fetch polyfill (e.g., node-fetch) in your application bootstrap before importing the SDK.
import Together, { type ClientOptions } from './src/together/index';
const client = new Together(options?: ClientOptions);
The primary entry point. ClientOptions accepts apiKey, baseURL, maxRetries (default 2), timeout, and defaultHeaders. Instantiate once and reuse across your application. All API resources are available as properties: client.chat, client.completions, client.embeddings, client.images, client.audio, client.files, client.fineTuning, client.models, client.endpoints, client.evals, client.batches, client.rerank, client.videos, client.codeInterpreter, client.beta.
import { APIError, BadRequestError, RateLimitError, AuthenticationError } from './src/together/index';
try {
await client.chat.completions.create({ ... });
} catch (err) {
if (err instanceof APIError) {
console.log(err.status, err.name, err.headers);
}
}
Base class for all HTTP-level errors thrown by the SDK. Use instanceof checks against specific subclasses (BadRequestError, RateLimitError, AuthenticationError, NotFoundError, InternalServerError, etc.) for granular handling. The status property holds the HTTP status code.
import { APIPromise } from './src/together/index';
Wraps every API call. It extends Promise and provides .withResponse() to get both the parsed body and the raw Response object, and .asResponse() to get only the raw HTTP response. Use .withResponse() when you need response headers (e.g., rate-limit metadata).
Send a user message to a hosted LLM and read the full response synchronously.
import Together from './src/together/index';
const client = new Together({
apiKey: process.env.TOGETHER_API_KEY,
});
async function main() {
const result = await client.chat.completions.create({
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
messages: [{ role: 'user', content: 'Explain quantum entanglement in one paragraph.' }],
});
console.log(result.choices[0]?.message?.content);
}
main();
Stream tokens as they are generated and print each chunk incrementally.
import Together from './src/together/index';
const client = new Together({
apiKey: process.env.TOGETHER_API_KEY,
});
async function streamChat() {
const stream = await client.chat.completions.create({
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
messages: [{ role: 'user', content: 'Write a haiku about TypeScript.' }],
stream: true,
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
}
console.log();
}
streamChat();
Generate a vector embedding for a string of text, useful for semantic search or retrieval-augmented generation.
import Together from './src/together/index';
import type { Embedding } from './src/together/resources/index';
const client = new Together({ apiKey: process.env.TOGETHER_API_KEY });
async function embed(text: string): Promise<number[]> {
const response = await client.embeddings.create({
model: 'togethercomputer/m2-bert-80M-8k-retrieval',
input: text,
});
const embedding: Embedding = response.data[0];
return embedding.embedding;
}
embed('The quick brown fox').then((vec) => console.log('Dimensions:', vec.length));
Handle API errors explicitly when you need deterministic retry logic of your own.
import Together, { APIError, RateLimitError } from './src/together/index';
const client = new Together({
apiKey: process.env.TOGETHER_API_KEY,
maxRetries: 0,
});
async function safeFetch() {
try {
const result = await client.completions.create({
model: 'mistralai/Mistral-7B-Instruct-v0.1',
prompt: 'Once upon a time',
max_tokens: 64,
});
console.log(result.choices[0]?.text);
} catch (err) {
if (err instanceof RateLimitError) {
console.warn('Rate limited. Back off and retry.');
} else if (err instanceof APIError) {
console.error(`API error ${err.status}: ${err.name}`);
} else {
throw err;
}
}
}
safeFetch();
source/client.ts - Defines the Together class, wires all resource sub-clients, and reads TOGETHER_API_KEY from the environment.source/index.ts - Single public entry point; re-exports the client, error classes, APIPromise, toFile, and Uploadable.source/resource.ts - Abstract APIResource base that holds a reference to the parent client.source/resources.ts - Barrel re-export of every resource namespace for tree-shaking-friendly imports.source/error.ts - Thin re-export shim pointing to core/error.ts.source/streaming.ts - Thin re-export shim for streaming utilities.source/uploads.ts - Thin re-export shim for toFile / Uploadable.source/version.ts - Exports the SDK version string constant.source/api-promise.ts - Thin re-export shim for APIPromise.source/core/ - Self-contained core: APIPromise, typed APIError hierarchy, streaming SSE decoder, and multipart upload construction.source/internal/ - Low-level utilities: HTTP header normalization, query string serialization, base64 encode/decode, environment variable reading, logging, UUID generation, platform detection, and binary byte helpers.source/lib/ - High-level abstractions: AbstractChatCompletionRunner, ChatCompletionRunner, ChatCompletionStream, ChatCompletionStreamingRunner, RunnableFunction, JSON schema helpers, and file-check utilities for function-calling workflows.source/resources/audio/ - Speech synthesis, transcription, translation, and voice listing endpoints.source/resources/beta/ - Beta endpoints for managed clusters, cluster storage, and Jig deployment orchestration.source/resources/chat/ - Chat completions resource with streaming and non-streaming overloads.source/resources/models/ - Model listing and retrieval.source/resources/code-interpreter/ - Remote code execution via the Together code interpreter API.TOGETHER_API_KEY: The client throws AuthenticationError (401) at runtime if the key is absent. Always verify process.env.TOGETHER_API_KEY is set before instantiation.fetch: Install node-fetch and assign globalThis.fetch = fetch before importing the SDK, or upgrade to Node 18+."module": "NodeNext": Ensure "moduleResolution": "NodeNext" is also set and that all relative imports within source/ include explicit .js extensions as generated.break out of a for await loop early, call stream.controller.abort() to release the underlying HTTP connection and avoid resource leaks.toFile required for binary uploads: When uploading audio or training files, always wrap Buffer or Blob values with toFile from source/index.ts; passing raw buffers directly will cause a runtime type error.maxRetries: 2 retries on 429 by default: If your use case requires immediate failure on rate limits (e.g., for custom backoff), set maxRetries: 0 in ClientOptions.I have dropped the Together AI TypeScript SDK source into `src/together/` in my project.
The integration guide is in `USAGE.md`. The upstream package is `user@example.com`.
Please help me integrate this SDK into my existing TypeScript/Node.js project step by step:
1. Read `USAGE.md` and `src/together/index.ts` to understand what is exported.
2. Update `tsconfig.json` so the project can import from `src/together/index`.
3. Create a singleton client module that reads `TOGETHER_API_KEY` from the environment.
4. Add a chat completion helper function that accepts a message string and returns the assistant reply.
5. Add a streaming variant of that function that yields tokens as they arrive.
6. Add error handling using the `APIError` subclasses from `src/together/index.ts`.
7. Show me the final files I need to create or modify, with full code.
Use only exports that are visible in `src/together/index.ts` and `src/together/resources/index.ts`.
Do not install the `together-ai` npm package; use the local source directly.
The Together AI TypeScript SDK is released under the MIT license (see source/LICENSE if present, or the repository at https://github.com/togethercomputer/together-typescript). This block redistributes the SDK source from the user@example.com npm package. Refer to the upstream package for the authoritative license text and contribution guidelines.
This product’s unique contribution is clean room implementation of valuable code block into AVCP compatible standard.
The full install guide and integration prompts unlock after purchase.
SaaS, AI & Subscription Products
Free