Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Last active June 7, 2022 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyspiro/30217c0955585ae0affb7dc4878869ae to your computer and use it in GitHub Desktop.
Save tonyspiro/30217c0955585ae0affb7dc4878869ae to your computer and use it in GitHub Desktop.
Get Objects from Cosmic by slug
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'simple-react-blog', // YOUR_BUCKET_SLUG
read_key: 'bucket-read-key' // YOUR_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
slug: 'a-wonderful-blog-post-about-earth'
},
props: 'slug,title,metadata.hero.imgix_url,metadata.author.title' // declare your response properties
})
const post = data.objects[0]
console.log(JSON.stringify(post, null, 2))
}
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment