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.
| Property | Type | Description |
|---|---|---|
message | string | Human-readable error message |
code | string | Machine-readable error code |
status | number | HTTP status code |
details | object | undefined | Additional 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
| Code | HTTP Status | Description |
|---|---|---|
missing_api_key | 401 | No API key provided |
invalid_api_key | 401 | API key is invalid or revoked |
no_active_subscription | 403 | No active subscription |
insufficient_credits | 402 | Not enough credits |
invalid_params | 400 | Missing or invalid parameters |
generation_failed | 502 | AI generation failed (credits refunded) |
internal_error | 500 | Unexpected 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');
}
}
}