Authentication

Learn how to authenticate your requests to the Cosmic API.

Get your API keys

Cosmic uses API keys to authenticate requests. For the following examples, you will need your:

  1. Bucket slug
  2. Bucket read key
  3. Bucket write key

You can get your API access keys by going to Bucket Settings > API Access in the Cosmic dashboard.

API keys image

Use your API keys

Use the methods below to use your Cosmic API keys.

GET
/v3/buckets/:bucket_slug/objects
// Import
import { createBucketClient } from '@cosmicjs/sdk';

// Authenticate
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: 'Here is the blog post content...',
    seo_description: 'This is the blog post SEO description.',
    featured_post: true,
  },
});