Media API
Guide for managing media files in your Cosmic bucket.
Overview
Media tools allow you to manage images, videos, documents, and other files in your Cosmic bucket. All media files are automatically optimized and served through Cosmic's CDN.
Available Tools
list_media
List media files in your bucket with filtering and pagination.
Use Cases:
- Browse uploaded images
- Find media by folder
- Paginate through large media libraries
- Filter by file type
Parameters:
limit
(optional): Number of files to return (max 1000)skip
(optional): Skip files for paginationfolder
(optional): Filter by folder pathsort
(optional): Sort field and direction
Example:
json
{
"folder": "blog-images",
"limit": 20,
"sort": "-created_at"
}
get_media
Get details about a specific media file.
Use Cases:
- Get image URLs and metadata
- Check file size and dimensions
- Retrieve optimization settings
- Get CDN URLs
Parameters:
id
(required): Media file ID
Example:
json
{
"id": "64f8a123b456c789"
}
upload_media
Upload new media files to your bucket.
Use Cases:
- Add images to blog posts
- Upload product photos
- Store documents and PDFs
- Upload video content
Parameters:
file
(required): File data (base64 encoded)filename
(required): Original filenamefolder
(optional): Folder to upload tometadata
(optional): Additional metadata
Example:
json
{
"file": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...",
"filename": "hero-image.jpg",
"folder": "homepage",
"metadata": {
"alt_text": "Hero image for homepage",
"caption": "Beautiful sunset over mountains",
"photographer": "John Doe"
}
}
File Types Supported
Images
- JPEG (.jpg, .jpeg) - Photos and complex images
- PNG (.png) - Images with transparency
- GIF (.gif) - Animated images
- WebP (.webp) - Modern format with better compression
- SVG (.svg) - Vector graphics
Videos
- MP4 (.mp4) - Most common video format
- WebM (.webm) - Web-optimized video
- MOV (.mov) - QuickTime format
Documents
- PDF (.pdf) - Documents and presentations
- DOC/DOCX (.doc, .docx) - Word documents
- TXT (.txt) - Plain text files
Other
- ZIP (.zip) - Compressed archives
- JSON (.json) - Data files
- CSV (.csv) - Spreadsheet data
Image Optimization
Cosmic automatically optimizes images and provides powerful transformation options:
URL Transformations
https://cosmic-s3.imgix.net/your-image.jpg?w=800&h=600&fit=crop
Common Parameters:
w
- Width in pixelsh
- Height in pixelsfit
- Resize behavior (crop, scale, fill)q
- Quality (1-100)fm
- Format (jpg, png, webp, auto)auto
- Auto-optimize (compress, format)
Responsive Images
html
<img
src="https://cosmic-s3.imgix.net/image.jpg?w=800&auto=compress,format"
srcset="
https://cosmic-s3.imgix.net/image.jpg?w=400&auto=compress,format 400w,
https://cosmic-s3.imgix.net/image.jpg?w=800&auto=compress,format 800w,
https://cosmic-s3.imgix.net/image.jpg?w=1200&auto=compress,format 1200w
"
sizes="(max-width: 400px) 400px, (max-width: 800px) 800px, 1200px"
alt="Optimized image"
/>
File Organization
Folder Structure
Organize your media with folders:
/
├── blog-images/
│ ├── 2024/
│ └── featured/
├── products/
│ ├── electronics/
│ └── clothing/
└── assets/
├── icons/
└── logos/
Naming Conventions
Good practices:
- Use descriptive names:
product-hero-image.jpg
- Include dates:
2024-01-15-event-photo.jpg
- Use hyphens instead of spaces:
team-photo.jpg
- Include dimensions:
logo-300x100.png
Avoid:
- Special characters:
image@#$.jpg
- Spaces:
my image.jpg
- Very long names:
this-is-a-very-long-filename-that-is-hard-to-read.jpg
Common Patterns
Blog Image Workflow
- Upload:
upload_media
with blog-specific folder - Get URL: Use returned URL in blog post content
- Optimize: Add URL parameters for responsive images
Product Gallery
- Upload multiple:
upload_media
for each product image - Organize: Use product-specific folders
- Retrieve:
list_media
with folder filter
Asset Management
- List by type: Filter media by folder
- Update metadata: Add alt text and captions
- Clean up: Remove unused files periodically
Error Handling
Common issues and solutions:
- File too large: Check file size limits (usually 10MB)
- Invalid format: Verify file type is supported
- Upload failed: Check network connection and API key
- Not found: Verify media ID exists
Best Practices
Performance
- Optimize images: Use appropriate formats and sizes
- Use CDN: Always use Cosmic's CDN URLs
- Lazy loading: Implement lazy loading for images
- WebP format: Use modern formats when possible
Organization
- Folder structure: Keep media organized in folders
- Naming: Use consistent, descriptive naming
- Metadata: Add alt text and captions for accessibility
- Cleanup: Remove unused media regularly
Security
- File validation: Validate file types on upload
- Size limits: Enforce reasonable file size limits
- Access control: Use appropriate API keys
Related
- Objects API - Managing content objects
- Types API - Object type management
- Complete Reference - All tools documentation