Handling Base64 Images in API Responses

Decode, preview, and debug image fields returned by REST and GraphQL APIs.

Many REST and GraphQL APIs return images as Base64-encoded strings inside JSON fields. This avoids separate file download endpoints but requires decoding on the client side. Common in identity verification, document processing, avatar upload, and AI/ML image APIs.

Typical API response shapes

APIs return Base64 in different formats. Always check your API documentation for the expected structure.

// Raw Base64 string
{
  "id": "img_123",
  "format": "png",
  "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB..."
}

// Full data URI
{
  "thumbnail": "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
}

// Nested object
{
  "result": {
    "image": {
      "base64": "iVBORw0KGgo...",
      "mimeType": "image/png"
    }
  }
}

Preview in JavaScript

Extract the field and render it immediately to verify the API returns valid image data.

async function previewApiImage(response) {
  const json = await response.json();
  const raw = json.data; // adjust field name
  const mime = json.format === 'jpg' ? 'image/jpeg' : `image/${json.format}`;
  const dataUri = raw.startsWith('data:') ? raw : `data:${mime};base64,${raw}`;

  document.getElementById('preview').src = dataUri;
}

Quick debugging with the online tool

When an API image looks wrong, copy the Base64 field value and paste it into our Base64 to Image decoder. The tool shows MIME type, dimensions, and file size — helping you spot truncated data, wrong encoding, or incorrect MIME types without writing debug code.

If the string fails to decode, check for: URL-safe Base64 variants (- and _ instead of + and /), missing padding (=), JSON escaping of special characters, or gzip-compressed payloads mistaken for image data.

Security note

Never execute or eval Base64 content from untrusted APIs. Treat it as binary image data only. Validate MIME types and size limits before rendering to prevent abuse.

Try it online — no code required

Use our free converter to test Base64 strings instantly in your browser.

Preview API Base64 output

More developer guides