> ## Documentation Index
> Fetch the complete documentation index at: https://buttercms.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Media field

> Detailed explanation of the Media field in ButterCMS, including input, output, configuration options, use cases, and the ButterCMS Media Library

The **Media** field allows you to add media from your Media Library—including images, documents, PDFs, audio, and videos—to your Pages and Collections.
\[!TIP] If you select an image with the Media field, you'll have the option to then add alt text, which can be included in the API response.

## Field at a glance

### Input and output

> [card] Input type
>
> File picker with search and preview (select from Media Library)

> [card] API output
>
> * without alt text: `string` (URL to asset in Media Library CDN)
> * with alt text: `object` with two key-value pairs: `url` and `alt`

### API response example

#### Without alt text

```json theme={null}
{
  "hero_image": "https://cdn.buttercms.com/abc123xyz"
}
```

#### With alt text

> [!TIP]
>
> Include `alt_media_text=1` in your API request to fetch assets with alt text.

```json theme={null}
{
  "hero_image": {
    "url": "https://cdn.buttercms.com/abc123xyz",
    "alt": "Team collaboration meeting in modern office"
  }
}
```

### Write API input

The Write API accepts the same two shapes: a URL string, or an object with `url` and an optional `alt` (up to 255 characters). The URL is downloaded to your Media Library and the alt text is stored on the field.

```json theme={null}
{
  "hero_image": {
    "url": "https://example.com/team.jpg",
    "alt": "Team collaboration meeting in modern office"
  }
}
```

### Field configuration options

> [!TIP]
>
> Checking the `default value` box in the field configuration options will allow you to set a default Media asset for the field.

| `Option name` | `Type` | `Function` |
| - | - | - |
| Required? | boolean | Make field required to save page |
| Help text | string | Add help text to describe usage to your team |
| Default value | boolean | Set a default value for the field |

## Common use cases

> [card] Page content
>
> * Hero/banner images
> * Featured images
> * Author photos
> * Thumbnails
> * Background images

> [card] Products
>
> * Product photos
> * Gallery images
> * Variant images
> * Size charts
> * Brand logos

> [card] Blogs & articles
>
> * Featured images
> * Inline content images
> * Author avatars
> * Social sharing images

> [card] Marketing
>
> * Ad creatives
> * Promotional banners
> * Logo variations
> * Icons and badges

## Key features

Explore key features of the Media Field, the ButterCMS Media Library, and our Image CDN.

> [card] [Media library overview](https://buttercms.com/docs/create-content/media-management/media-library-overview.md)
>
> Explore your ButterCMS media library, including sorting, uploading, searching, and tagging media.

> [card] [Alt text for accessibility and SEO](https://buttercms.com/docs/create-content/media-management/alt-text.md)
>
> How to add alt text to images and retrieve from our API.

> [card] [CDN & performance](https://buttercms.com/docs/build-your-app/media-cdn/customizing-media-urls.md)
>
> Learn about our Media CDN, including how to customize your URLs

> [card] [Image transformations](https://buttercms.com/docs/create-content/media-management/image-editing-transformations.md)
>
> How to transform your images: flip, rotate, resize, crop, add filters and effects, and more!

> [card] [Dynamic image resizing](https://buttercms.com/docs/create-content/media-management/image-editing-transformations.md#responsive-images)
>
> How to leverage our CDN's `resize` task to create responsive images in your app.

> [card] [Automatic image compression](https://buttercms.com/docs/create-content/media-management/automatic-image-compression-webp.md)
>
> Learn about ButterCMS's automatic image compression, including how to switch back to the original image size and format.

## Best practices

1. **Always add alt text to images**: Critical for accessibility and SEO
2. **Optimize before upload**: Compress images using tools like TinyPNG
3. **Use appropriate dimensions**: Don't upload 4000px images for thumbnails
4. **Leverage transformations**: Use URL parameters for responsive images
5. **Consider lazy loading**: Load images only when needed
6. **Test WebP support**: Ensure fallbacks for older browsers

## Working with images in your application

> [tab] JavaScript/React
>
> ```javascript theme={null}
> import Butter from 'buttercms';
> const butter = Butter('your-api-key');
>
> // Fetch with alt text
> const response = await butter.page.retrieve('*', 'landing-page', {
>   alt_media_text: 1
> });
>
> const page = response.data.data.fields;
>
> // React component with responsive images
> function HeroImage({ image }) {
>   // If alt text is enabled
>   const imageUrl = typeof image === 'object' ? image.url : image;
>   const altText = typeof image === 'object' ? image.alt : '';
>
>   return (
>     <img
>       src={imageUrl}
>       srcSet={`
>         ${imageUrl.replace('cdn.buttercms.com/', 'cdn.buttercms.com/resize=width:640/')} 640w,
>         ${imageUrl.replace('cdn.buttercms.com/', 'cdn.buttercms.com/resize=width:1280/')} 1280w,
>         ${imageUrl} 1920w
>       `}
>       sizes="100vw"
>       alt={altText}
>       loading="lazy"
>     />
>   );
> }
> ```

> [tab] Next.js with Image Optimization
>
> ```javascript theme={null}
> import Image from 'next/image';
>
> function ProductImage({ product }) {
>   return (
>     <Image
>       src={product.image}
>       alt={product.image_alt || product.name}
>       width={800}
>       height={600}
>       placeholder="blur"
>       blurDataURL={`${product.image}?blur=50`}
>     />
>   );
> }
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.