Skip to main content

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

ParameterTypeRequiredDefaultDescription
apiSecretstringYesSecret API key. Never expose client-side.
environment'production' | 'sandbox'No'production'Target environment. Use 'sandbox' for development.
urlstringNoOverride the GraphQL endpoint URL. Useful for self-hosted deployments.
enableTelemetrybooleanNotrueSend anonymized usage data to AccruPay. See Telemetry.
onAuthError(error: Error) => voidNoCalled on HTTP 401 / UNAUTHENTICATED responses.
onGraphQLError(errors: GraphQLError[]) => voidNoCalled when response.errors[] is non-empty.
onNetworkError(error: Error) => voidNoCalled on transport-level failures (DNS, timeout, etc.).
tip

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);
}
danger

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');
}
warning

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);
}