Query Objects and Media in Cosmic
find method of the objects or media clients. If accessing the API via a browser URL, curly braces {} must be URL-encoded as %7B and %7D.website·Indexed 36 days ago
https://docs-v3.cosmicjs.com/Documentation for Cosmic, a headless CMS. Includes guides for the admin dashboard, project and bucket management, and a comprehensive REST API reference covering authentication, object types, queries, media, webhooks, and request limits.
find method of the objects or media clients. If accessing the API via a browser URL, curly braces {} must be URL-encoded as %7B and %7D.message field provides specific details about the failure, such as validation errors for metafields.{
"status": 400,
"message": "Metafield validation: invalid 'value' for metafield with key: 'number' and type: 'number'"
}slug used for API requests. Optional features for Object types include emojis for dashboard organization, localization for multi-language support, and preview links for live site testing.Objects are the fundamental building blocks of content in a Cosmic Bucket. Each Object contains core system properties and custom Metafields.
Core Properties:
id: Unique identifier for the Object.slug: Unique identifier per Object type. Localized versions share the same slug.title: The title of the Object.type: The slug of the Object type.status: Current state (published or draft).metadata: A JSON object containing the values of the Metafields defined for the Object type.created_at / modified_at: Timestamps for creation and last modification.published_at: Timestamp of the last publication.publish_at / unpublish_at: UNIX millisecond timestamps for scheduled publishing/unpublishing.modified_by: ID of the user who last modified the Object.Note: The HTML Content property is deprecated in API v3; use Metafields instead.
{
"id": "63dc57ca24090e0008683d42",
"slug": "add-a-headless-cms-to-astro-in-3-easy-steps",
"title": "Add a headless CMS to Astro in 3 easy steps",
"bucket": "63dc24a4d71e244b63c88fca",
"created_at": "2023-02-03T00:39:38.35Z",
"modified_at": "2023-03-23T12:08:13.547Z",
"status": "published",
"thumbnail": "https://imgix.cosmicjs.com/...",
"published_at": "2023-03-23T12:08:13.547Z",
"modified_by": "629e6cdda6f4f100091ae2e0",
"publish_at": null,
"type": "blog-posts",
"metadata": {
"content": "Astro is a lightweight web framework...",
"image": {
"url": "https://cdn.cosmicjs.com/..."
},
"published_date": "2022-09-20",
"author": {
"title": "Bill Brasky"
}
}
}Extensions are iframes that connect third-party APIs to the Cosmic dashboard. Requirements: The URL must be served via https:// and have X-Frame Options enabled. Extensions can be placed in the following locations:
POST requests to third-party services when specific events occur in a Bucket. To create a webhook, navigate to Bucket > Settings > Webhooks and select "Add Webhook". For testing and viewing response data, tools like Beeceptor are recommended.metadata object during a create or update request. Use the key:value format where the key matches the Metafield key defined in the Object type.await cosmic.objects.updateOne('object-id', {
metadata: {
headline: 'This guitar amp is LOUD!',
max_volume: 11,
on_sale: true
}
})
Content in Cosmic is structured using Object types and individual Objects:
Promote content to move selected Objects from one Bucket to another within the same Project. This is typically used for staging-to-production workflows.
Ways to initiate promotion:
What is copied:
Permissions: Requires write access on both source and target Buckets. Schema-altering actions may require a Project admin role.
Navigate to Workspace > Usage to track resource consumption. Available tools include:
Cosmic uses Bucket slugs and API keys to authenticate requests for content management. You can find these credentials in the Cosmic dashboard under Bucket Settings > API Access.
Security Warning: Never expose your Bucket write key in client-side code.
import { createBucketClient } from '@cosmicjs/sdk';
const cosmic = createBucketClient({
bucketSlug: 'BUCKET_SLUG',
readKey: 'BUCKET_READ_KEY',
writeKey: 'BUCKET_WRITE_KEY',
});
// Fetch content
await cosmic.objects.find({ type: 'posts' }).limit(1);
// Write content
await cosmic.objects.insertOne({
title: 'Blog Post Title',
type: 'posts',
metadata: { content: '...' },
});id, not its slug.const query = {
type: 'posts',
'metadata.category': 'category-object-id', // Use Object ID here
};
await cosmic.objects.find(query);