MakePhotos Docs
SDK Reference

Error Handling

Handle errors from the MakePhotos SDK with typed error classes.

Error Handling

The SDK throws typed errors that you can catch and handle precisely.

Error classes

import {
  MakePhotosError,
  AuthenticationError,
  InsufficientCreditsError,
} from 'makephotos';

MakePhotosError

Base error class. All API errors extend this.

PropertyTypeDescription
messagestringHuman-readable error message
codestringMachine-readable error code
statusnumberHTTP status code
detailsobject | undefinedAdditional context

AuthenticationError

Thrown when the API key is missing, invalid, or revoked. Extends MakePhotosError.

InsufficientCreditsError

Thrown when you don't have enough credits. Extends MakePhotosError. The details object contains monthly_remaining and topup_remaining.

Usage

try {
  const result = await client.generate.create({
    imageUrl: 'https://example.com/product.jpg',
    type: 'product',
    style: 'studio',
  });
} catch (err) {
  if (err instanceof InsufficientCreditsError) {
    console.log('Need more credits:', err.details);
    // { monthly_remaining: 0, topup_remaining: 0 }
  } else if (err instanceof AuthenticationError) {
    console.log('Invalid API key');
  } else if (err instanceof MakePhotosError) {
    console.log(`API error [${err.code}]: ${err.message}`);
  } else {
    throw err; // unexpected error
  }
}

Error codes

CodeHTTP StatusDescription
missing_api_key401No API key provided
invalid_api_key401API key is invalid or revoked
no_active_subscription403No active subscription
insufficient_credits402Not enough credits
invalid_params400Missing or invalid parameters
generation_failed502AI generation failed (credits refunded)
internal_error500Unexpected server error

Generation failure and refunds

If the AI model fails during generation, credits are automatically refunded. The error includes a details.refunded boolean:

try {
  await client.generate.create({ ... });
} catch (err) {
  if (err instanceof MakePhotosError && err.code === 'generation_failed') {
    if (err.details?.refunded) {
      console.log('Credits were refunded — try again');
    }
  }
}

On this page