Errors and payments
Handle SDK errors without losing the original failure.
The SDK exports DevaError and specific error classes. For ordinary requests, inspect the class and available fields instead of matching message text.
| Error | Meaning |
|---|---|
AuthenticationError | Authentication failed, including HTTP 401 |
PermissionError | Access denied, including HTTP 403 |
InvalidRequestError | Invalid request, including HTTP 400 |
InsufficientQuotaError | Quota or balance failure, including HTTP 402 without a parsed payment challenge |
RateLimitError | Rate limit, including HTTP 429 |
ServerError | Server failure, including HTTP 5xx |
X402PaymentRequiredError | An ordinary SDK request received a parsed payment challenge |
Other failures can be DevaError. Fields such as status, code, balance, and required are optional.
import { DevaClient, DevaError, X402PaymentRequiredError } from "@deva-me/agent-sdk";
const apiKey = process.env.DEVA_API_KEY;
const model = process.env.DEVA_MODEL;
if (!apiKey || !model) throw new Error("Set DEVA_API_KEY and DEVA_MODEL");
const client = new DevaClient({ apiKey });
try {
await client.chat.create({
model,
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
if (error instanceof X402PaymentRequiredError) {
console.error("Payment required; review the challenge before paying.");
} else if (error instanceof DevaError) {
console.error("Deva request failed", error.status, error.code);
}
throw error;
}Payment challenges
For ordinary requests, X402PaymentRequiredError.paymentChallenge contains the parsed challenge when one is returned. Wallet auto-payment is disabled unless explicitly configured. Enabling it requires a local policy with amount limits, approved networks, and approved payees.
Review the SDK's payment configuration before enabling payments. Do not copy a recipient address from an unrelated example.
Streaming difference
chat.stream() uses the raw response path. Its HTTP 402 errors are classified as quota errors; that path does not preserve the payment challenge or run the ordinary request payment flow.
Sources: error classes, request handling, stream handling.