Back to blog
Blog

New docs examples

Cosmic's avatar

Cosmic

June 07, 2022

cover image

We've added new examples to the Cosmic documentation to help you learn common Cosmic API methods and use cases. The new examples feature simple copy + paste code for Node.js, cURL, GraphQL, and the Cosmic CLI.

You can run the following code as is or add your Bucket access keys to connect to your Bucket content. Learn all of the available examples and follow along with the code examples below which use the Cosmic NPM module.


Go to the Cosmic docs to check out the new examples.


Setup & running Node.js

To run the following code examples, make sure you have Node.js installed on your machine. Then run the following setup commands in your terminal to make the Cosmic NPM module available in a new Node.js project:

mkdir cosmic-example
cd cosmic-example
yarn add cosmicjs

Add any of the code examples below to an index.js file and run node to see the results:

node index.js


Basic queries

The basic queries show you how to get a list of Objects in your Bucket by Object Type as well as getting a single Object in your Bucket by Object slug.

Get Objects by type:

Get single Object by slug:

Advanced queries

The advanced queries show you some of the capabilities to get content by more detailed search options such as metadata values, $gte (greater than or equal to), $lte(less than or equal to), $ne (not equal to), and more.

Search Objects by metadata value:

Search Objects by metadata number value less than or equal to using $lte:

Search Objects not equal to metadata value using $ne:

Check out more examples of advanced queries in the docs.


Other Examples

Other examples include adding a new Object and uploading media using React Dropzone.  Copy + paste the code, add your Bucket access keys, and run the code to learn how to add content to your Cosmic Bucket.


We hope you enjoy these additions to our extensive documentation to help you better implement Cosmic-powered content into your websites and apps. Check out the Cosmic docs examples section and let us know what you think!