This is the Official Cosmic JS JavaScript Client which allows you to easily create, read, update and delete content from your Cosmic JS Buckets. Includes cosmicjs.browser.min.js
for easy integration in the browser.
Getting started
Go to https://cosmicjs.com, create an account and set up a Bucket.
Install
npm install cosmicjs
Or include in an HTML file
Usage
Authentication
Use your Cosmic JS account email and password to create an authentication token. At this time, authentication is only necessary for adding Buckets.
const Cosmic = // empty initCosmic
Buckets
Add Bucket
Add a new Bucket to your account.
const Cosmic = token: 'your-token-from-auth-request' // from Cosmic.authenticateCosmic
Connect to Bucket
Use the Cosmic.bucket
method to connect to different Buckets in your account. If you would like to restrict read and write access to your Bucket, you can do so in Your Bucket > Basic Settings in your Cosmic JS Dashboard.
// Use the Cosmic.bucket method to connect to different Buckets in your account.const Cosmic = token: 'your-token-from-auth-request' // optionalconst bucket = Cosmicconst bucket2 = Cosmic
Get Bucket
Returns the entire Bucket including Object Types, Objects, Media and more.
bucket
Object Types
Add Object Type
Add a new Object Type to your Bucket.
const params = title: 'Pages' singular: 'Page' slug: 'pages' metafields: type: 'text' title: 'Headline' key: 'headline' required: true type: 'file' title: 'Hero' key: 'hero' required: true bucket
Get Object Types
Get all Object Types in your Bucket.
bucket
Edit Object Type
Edit an existing Object Type in your Bucket.
bucket
Delete Object Type
Delete an existing Object Type from your Bucket. *This does not delete Objects in this Object Type.
bucket
Objects
Add Object
Add a new Object to your Bucket.
const params = title: 'Cosmic JS Example' type_slug: 'examples' content: 'Learning the Cosmic JS API is really fun and so easy' metafields: key: 'Headline' type: 'text' value: 'Learn Cosmic JS!' key: 'Author' type: 'text' value: 'Quasar Jones' options: slug_field: false bucket
Get Objects
Returns all Objects from your Bucket.
bucket
Get Objects by Type
Get Objects from an Object Type.
const params = type_slug: 'posts' limit: 5 skip: 0 sort: '-created_at' // optional, if sort is needed. (use one option from 'created_at,-created_at,modified_at,-modified_at,random') locale: 'en' // optional, if localization set on Objects status: 'all' // optional, if need to get draft Objectsbucket
Search Objects
Search Objects in an Object Type.
// Search by connected Object Metafieldconst params = type_slug: 'posts' metafield_key: 'author' metafield_object_slug: 'carson-gibbons' limit: 5 skip: 0 sort: '-created_at' // optional, if sort is needed. (use one option from 'created_at,-created_at,modified_at,-modified_at,random') locale: 'en' // optional, if localization set on Objects, status: 'all' // optional, if need to get draft Objects,// Search by Metafield Valueconst params = type_slug: 'posts' metafield_key: 'headline' metafield_value: 'Hello World' limit: 5 skip: 0 sort: '-created_at' // optional, if sort is needed. (use one option from 'created_at,-created_at,modified_at,-modified_at,random')// Search by Metafield Has Valueconst params = type_slug: 'posts' metafield_key: 'headline' metafield_has_value: 'World' limit: 5 skip: 0 sort: '-created_at' // optional, if sort is needed. (use one option from 'created_at,-created_at,modified_at,-modified_at,random')bucket
Edit Object
Edit an existing Object in your Bucket.
bucket
Delete Object
Delete an existing Object in your Bucket.
bucket
Media
Add Media
The only required post value is media which is the name of your media sent.
bucket
Get Media
You can add the folder
parameter to get Media from a certain folder. You can use the full Imgix suite of image processing tools on the URL provided by the imgix_url
property value. Check out the Imgix documentation for more info.
bucket
Delete Media
bucket
Webhooks
Add Webhook
Sends a POST request to the endpoint of your choice when the event occurs. The data payload in the same fomat as Object and Media. Read more about Webhooks including the payload sent to the endpoint on the Webhooks documentation page.
bucket
Delete Webhook
bucket
Extensions
Add Extension
Adds an Extension to your Bucket. Required post values include zip which is the name of your file sent. Read more about Extensions on the Extensions documentation page.
bucket
Delete Extension
bucket