Skip to main content
The SDK throws typed errors that you can catch and handle.

Error classes

The Node.js SDK exports:
  • MailrifyError
  • AuthenticationError
  • ValidationError
  • NotFoundError
  • RateLimitError
  • ApiError

Catch typed errors

import Mailrify, {
  ApiError,
  AuthenticationError,
  MailrifyError,
  NotFoundError,
  RateLimitError,
  ValidationError
} from 'mailrify';

const client = new Mailrify(process.env.MAILRIFY_SECRET_KEY as string);

try {
  await client.contacts.get('missing-contact-id');
} catch (error) {
  if (error instanceof NotFoundError) {
    console.error('Contact not found');
    return;
  }

  if (error instanceof ValidationError) {
    console.error('Validation failed', error.details);
    return;
  }

  if (error instanceof AuthenticationError) {
    console.error('Check API key type or value');
    return;
  }

  if (error instanceof RateLimitError) {
    console.error('Rate limited, retry after seconds:', error.retryAfter);
    return;
  }

  if (error instanceof ApiError) {
    console.error('Server/API error', error.status, error.details);
    return;
  }

  if (error instanceof MailrifyError) {
    console.error('Mailrify SDK error', error.status, error.message);
    return;
  }

  throw error;
}

Key-type mismatch example

events.track requires a public key and most other methods require a secret key.
const secretClient = new Mailrify(process.env.MAILRIFY_SECRET_KEY as string);

try {
  await secretClient.events.track({
    email: '[email protected]',
    event: 'signup'
  });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error(error.message);
  }
}

Retry and timeout behavior

The SDK automatically retries transient failures:
  • Up to 3 retries for 429 and 5xx responses
  • Uses exponential backoff when retry-after is not present
  • Throws timeout errors when request duration exceeds the configured timeout
You can customize timeout in client config:
const clientWithShortTimeout = new Mailrify(process.env.MAILRIFY_SECRET_KEY as string, {
  timeout: 10_000
});