Last active
June 7, 2022 06:32
-
-
Save tonyspiro/30217c0955585ae0affb7dc4878869ae to your computer and use it in GitHub Desktop.
Get Objects from Cosmic by slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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