Agentmail TypeScript Library
The Agentmail TypeScript library provides convenient access to the Agentmail APIs from TypeScript.
Table of Contents
- Installation
- Reference
- Usage
- Environments
- Request and Response Types
- Exception Handling
- Binary Response
- Advanced
- Contributing
Installation
npm i -s agentmail
Reference
A full reference for this library is available here.
Usage
Instantiate and use the client with the following:
import { AgentMailClient } from "agentmail";
const client = new AgentMailClient({ apiKey: "YOUR_API_KEY" });
await client.inboxes.create(undefined);
Environments
This SDK allows you to configure different environments for API requests.
import { AgentMailClient, AgentMailEnvironment } from "agentmail";
const client = new AgentMailClient({
environment: AgentMailEnvironment.Prod,
});
Request and Response Types
The SDK exports all request and response types as TypeScript interfaces. Simply import them with the following namespace:
import { AgentMail } from "agentmail";
const request: AgentMail.ListInboxesRequest = {
...
};
Exception Handling
When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.
import { AgentMailError } from "agentmail";
try {
await client.inboxes.create(...);
} catch (err) {
if (err instanceof AgentMailError) {
console.log(err.statusCode);
console.log(err.message);
console.log(err.body);
console.log(err.rawResponse);
}
}
Binary Response
You can consume binary data from endpoints using the BinaryResponse type which lets you choose how to consume the data:
const response = await client.domains.getZoneFile(...);
const stream: ReadableStream<Uint8Array> = response.stream();
// const arrayBuffer: ArrayBuffer = await response.arrayBuffer();
// const blob: Blob = response.blob();
// const bytes: Uint8Array = response.bytes();
// You can only use the response body once, so you must choose one of the above methods.
// If you want to check if the response body has been used, you can use the following property.
const bodyUsed = response.bodyUsed;
Save binary response to a file
Node.js
ReadableStream (most-efficient)
import { createWriteStream } from 'fs'; import { Readable } from 'stream'; import { pipeline } from 'stream/promises'; const response = await client.domains.getZoneFile(...); const stream = response.stream(); const nodeStream = Readable.fromWeb(stream); const writeStream = createWriteStream('path/to/file'); await pipeline(nodeStream, writeStream);ArrayBuffer
import { writeFile } from 'fs/promises'; const response = await client.domains.getZoneFile(...); const arrayBuffer = await response.arrayBuffer(); await writeFile('path/to/file', Buffer.from(arrayBuffer));Blob
import { writeFile } from 'fs/promises'; const response = await client.domains.getZoneFile(...); const blob = await response.blob(); const arrayBuffer = await blob.arrayBuffer(); await writeFile('output.bin', Buffer.from(arrayBuffer));Bytes (UIntArray8)
import { writeFile } from 'fs/promises'; const response = await client.domains.getZoneFile(...); const bytes = await response.bytes(); await writeFile('path/to/file', bytes);
Bun
ReadableStream (most-efficient)
const response = await client.domains.getZoneFile(...); const stream = response.stream(); await Bun.write('path/to/file', stream);ArrayBuffer
const response = await client.domains.getZoneFile(...); const arrayBuffer = await response.arrayBuffer(); await Bun.write('path/to/file', arrayBuffer);Blob
const response = await client.domains.getZoneFile(...); const blob = await response.blob(); await Bun.write('path/to/file', blob);Bytes (UIntArray8)
const response = await client.domains.getZoneFile(...); const bytes = await response.bytes(); await Bun.write('path/to/file', bytes);
Deno
ReadableStream (most-efficient)
const response = await client.domains.getZoneFile(...); const stream = response.stream(); const file = await Deno.open('path/to/file', { write: true, create: true }); await stream.pipeTo(file.writable);ArrayBuffer
const response = await client.domains.getZoneFile(...); const arrayBuffer = await response.arrayBuffer(); await Deno.writeFile('path/to/file', new Uint8Array(arrayBuffer));Blob
const response = await client.domains.getZoneFile(...); const blob = await response.blob(); const arrayBuffer = await blob.arrayBuffer(); await Deno.writeFile('path/to/file', new Uint8Array(arrayBuffer));Bytes (UIntArray8)
const response = await client.domains.getZoneFile(...); const bytes = await response.bytes(); await Deno.writeFile('path/to/file', bytes);
Browser
Blob (most-efficient)
const response = await client.domains.getZoneFile(...); const blob = await response.blob(); const url = URL.createObjectURL(blob); // trigger download const a = document.createElement('a'); a.href = url; a.download = 'filename'; a.click(); URL.revokeObjectURL(url);ReadableStream
const response = await client.domains.getZoneFile(...); const stream = response.stream(); const reader = stream.getReader(); const chunks = []; while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } const blob = new Blob(chunks); const url = URL.createObjectURL(blob); // trigger download const a = document.createElement('a'); a.href = url; a.download = 'filename'; a.click(); URL.revokeObjectURL(url);ArrayBuffer
const response = await client.domains.getZoneFile(...); const arrayBuffer = await response.arrayBuffer(); const blob = new Blob([arrayBuffer]); const url = URL.createObjectURL(blob); // trigger download const a = document.createElement('a'); a.href = url; a.download = 'filename'; a.click(); URL.revokeObjectURL(url);Bytes (UIntArray8)
const response = await client.domains.getZoneFile(...); const bytes = await response.bytes(); const blob = new Blob([bytes]); const url = URL.createObjectURL(blob); // trigger download const a = document.createElement('a'); a.href = url; a.download = 'filename'; a.click(); URL.revokeObjectURL(url);
Convert binary response to text
ReadableStream
const response = await client.doma