- Published
- Reading
- 3 min
- By
- Anlyon Team
The TypeScript SDK for agents that do things
One typed TypeScript client for letting an AI agent act on production: invoke an action by name, Anlyon holds the credential, and risky calls wait for a human.
- typescript
- sdk
- agents
- tutorial
The Anlyon TypeScript SDK is how your agent asks for something to happen in production. The agent invokes a named action. Anlyon holds the credential, waits for a human when the action needs one, and makes the call to Stripe, GitHub or your own API itself.
Installation
Install the SDK using your preferred package manager:
npm install @anlyonhq/sdk
# or
pnpm add @anlyonhq/sdk
# or
yarn add @anlyonhq/sdk
Quick Start
1. Initialize the Client
import { Client } from '@anlyonhq/sdk';
const client = new Client({
apiKey: process.env.ANLYON_API_KEY!,
});
2. Define an action (operator key, once)
Define the production call from an operator key, not the key your agent runs with. This is the only place the credential appears.
const ops = new Client({ apiKey: process.env.ANLYON_OPERATOR_KEY! });
await ops.secrets.put('STRIPE_KEY', {
value: process.env.STRIPE_KEY!,
allowedHosts: ['api.stripe.com'],
});
await ops.actions.create({
name: 'refund-order',
description: 'Refund a Stripe charge.',
method: 'POST',
urlTemplate: 'https://api.stripe.com/v1/refunds',
headers: { Authorization: 'Bearer {{secret:STRIPE_KEY}}', 'Content-Type': 'application/x-www-form-urlencoded' },
inputSchema: {
type: 'object',
properties: { charge: { type: 'string' }, amount: { type: 'integer' } },
required: ['charge', 'amount'],
},
requiresApproval: true,
});
3. Invoke it from the agent
const anlyon = new Client({ apiKey: process.env.ANLYON_AGENT_KEY! });
const { data } = await anlyon.actions.invoke(
'refund-order',
{ charge: 'ch_3P9x', amount: 12000 },
{ idempotencyKey: 'refund:ord_42' },
);
if (data!.pendingApproval) {
console.log('Parked for a human:', data!.approvalId);
} else {
console.log('Stripe replied', data!.responseStatus);
}
The agent's half carries no URL, header or key. With the idempotency key, a same-key, same-request invoke is answered from the recorded invocation. It is not executed again.
4. The local gate, for calls that can't move
When a call cannot become an HTTP action (an ORM write, a native SDK), wrap your own function instead:
import { ApprovalDeniedError } from '@anlyonhq/sdk';
const guardedRefund = client.approvals.gate(
{ title: 'Refund a customer?', timeoutMs: 10 * 60_000 },
refund,
);
try {
await guardedRefund('ord_42', 129.99); // blocks until a human approves
} catch (err) {
if (err instanceof ApprovalDeniedError) {
console.log('Denied:', err.approval.decisionNote);
}
}
This is the fallback path. Your process still runs your function with your credential, so you get the human decision and the record, not credential isolation.
Verify webhooks
Anlyon signs every webhook it sends you. @anlyonhq/sdk/nextjs and the other framework adapters verify the signature before your handler runs. See Verify webhook signatures.
Error Handling
The SDK provides typed error classes for different scenarios:
import {
AnlyonError,
AuthenticationError,
QuotaExceededError,
ValidationError
} from '@anlyonhq/sdk';
try {
await anlyon.actions.invoke('refund-order', { charge: 'ch_3P9x', amount: 12000 });
} catch (error) {
if (error instanceof QuotaExceededError) {
console.error('Quota exceeded:', error.message);
// Handle quota exceeded
} else if (error instanceof ValidationError) {
console.error('Validation error:', error.message);
// Handle validation error
} else if (error instanceof AuthenticationError) {
console.error('Authentication failed');
// Handle auth error
}
}
Best Practices
- Store API keys securely: Use environment variables, never commit them to version control
- Handle errors gracefully: Always wrap SDK calls in try-catch blocks
- Use TypeScript: The SDK is fully typed for better developer experience
- Key every side effect: Derive the idempotency key from the business operation. Anlyon does not retry an action call, and an invocation whose outcome is
unknownneeds reconciling, not retrying - Read the receipt: Its grade says how far the outcome was checked
Next Steps
- Read the full API documentation
- Read how to add human-in-the-loop approval to AI agent tool calls
- Read Actions in the docs
