Node SDK Configuration
Constructor
import AccruPay from '@accrupay/node';
const accrupay = new AccruPay({
apiSecret: process.env.ACCRUPAY_API_SECRET!,
environment: 'production',
enableTelemetry: true,
onAuthError: (error) => {
console.error('Auth failed — rotate API key:', error.message);
},
onGraphQLError: (errors) => {
for (const err of errors) {
console.error(`[GraphQL] ${err.message}`, err.extensions);
}
},
onNetworkError: (error) => {
console.error('Network error:', error.message);
},
});
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
apiSecret | string | Yes | — | Secret API key. Never expose client-side. |
environment | 'production' | 'sandbox' | No | 'production' | Target environment. Use 'sandbox' for development. |
url | string | No | — | Override the GraphQL endpoint URL. Useful for self-hosted deployments. |
enableTelemetry | boolean | No | true | Send anonymized usage data to AccruPay. See Telemetry. |
onAuthError | (error: Error) => void | No | — | Called on HTTP 401 / UNAUTHENTICATED responses. |
onGraphQLError | (errors: GraphQLError[]) => void | No | — | Called when response.errors[] is non-empty. |
onNetworkError | (error: Error) => void | No | — | Called on transport-level failures (DNS, timeout, etc.). |
Create a single AccruPay instance and export it as a module singleton. The client manages its own connection pool.
Error callbacks
onAuthError
Fires when the API returns an authentication failure (401 or UNAUTHENTICATED GraphQL extension code).
onAuthError: (error: Error) => {
// The error message contains the raw server response.
// Re-initialize the client with the new secret after rotation.
console.error('Authentication failed:', error.message);
}
A fired onAuthError means every subsequent call will also fail until you re-initialize the client with a valid apiSecret.
onGraphQLError
Fires when the GraphQL response body contains an errors array. Each GraphQLError object follows the GraphQL spec and includes an extensions field with AccruPay-specific error codes.
import type { GraphQLError } from 'graphql';
onGraphQLError: (errors: GraphQLError[]) => {
for (const err of errors) {
const code = err.extensions?.code as string | undefined;
switch (code) {
case 'VALIDATION_ERROR':
// Surface to caller
break;
default:
// Log and alert
console.error(`[${code}] ${err.message}`);
}
}
}
onNetworkError
Fires on transport-level failures before a GraphQL response is received (DNS resolution failure, connection refused, timeout).
onNetworkError: (error: Error) => {
// Implement retry / circuit-breaker logic here.
metrics.increment('accrupay.network_error');
}
onNetworkError does not fire for GraphQL-level errors (those go to onGraphQLError). Wire both callbacks to get complete error coverage.
Telemetry
When enableTelemetry is true (the default), the SDK sends:
- SDK version string
- Method names called (e.g.
clientSessions.payments.start) - Runtime environment identifier
No customer data, amounts, or billing fields are included. To opt out:
const accrupay = new AccruPay({
apiSecret: process.env.ACCRUPAY_API_SECRET!,
enableTelemetry: false,
});
TypeScript types
All constructor option types are exported from @accrupay/node:
import type { AccruPayConfig } from '@accrupay/node';
function buildClient(config: AccruPayConfig) {
return new AccruPay(config);
}