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
const Cosmic = require('cosmicjs')({ | |
token: 'your-token-from-auth-request' // required | |
}) | |
Cosmic.addBucket({ | |
title: 'My New Bucket', | |
slug: 'my-new-bucket' // must be unique across all Buckets in system | |
}).then(data => { | |
console.log(data) | |
}).catch(err => { | |
console.log(err) |
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
const bucket = Cosmic.bucket({ | |
slug: 'wedding-site', | |
read_key: '' | |
}) | |
bucket.getObjects({ | |
limit: 2 | |
}).then(data => { | |
console.log(data) | |
}).catch(err => { | |
console.log(err) |
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
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!" | |
}, |
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
const bucket = Cosmic.bucket({ | |
slug: 'bucket-slug', | |
write_key: '' | |
}) | |
bucket.addMedia({ | |
media: '<FILE_DATA>', | |
folder: 'your-folder-slug', | |
metadata: { | |
caption: 'Beautiful picture of the beach', | |
credit: 'Tyler Jackson' |
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
/* | |
Cosmic JS Bucket lifecycle methods | |
Go to https://cosmicjs.com to create an account and get your Auth token at | |
Your Account Settings > Authentication | |
*/ | |
const run = async () => { | |
const api = require('cosmicjs')({ | |
token: 'your-auth-token' // access token found in Your Account Settings > Authentication | |
}) |
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
const bucketSetupExample = async () => { | |
const Cosmic = require('cosmicjs') | |
const api = Cosmic({ | |
token: '' /* Your secret authentication token found in Account Settings > Authentication */ | |
}) | |
try { | |
const response = await api.addBucket({ | |
title: 'New Test Bucket', | |
slug: 'something-totally-unique' | |
}) |
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
# Install the Cosmic CLI | |
npm i -g cosmic-cli | |
cosmic login | |
# Node.js | |
cosmic init node-starter | |
# React | |
cosmic init react-starter |
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
require('dotenv').config() | |
const Cosmic = require('cosmicjs') | |
const api = Cosmic() | |
const async = require('async') | |
const algoliasearch = require('algoliasearch') | |
const client = algoliasearch(process.env.ALGOLIA_ACCOUNT, process.env.ALGOLIA_INDEX) | |
const index = client.initIndex('Stories') | |
const buckets = ['your-bucket-slug','another-bucket-slug'] // Add all Bucket slugs here | |
async.eachSeries(buckets, (bucket_slug, bucketEachCallback) => { |
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
require('dotenv').config() | |
const async = require('async') | |
const axios = require('axios') | |
const algoliasearch = require('algoliasearch') | |
const client = algoliasearch(process.env.ALGOLIA_ACCOUNT, process.env.ALGOLIA_INDEX) | |
const index = client.initIndex('Stories') | |
const getClaps = () => { | |
let hit_count = 0 | |
index.browse('', {}, function browseDone(err, content) { | |
if (err) { |
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
module.exports = function(req, res) { | |
const Cosmic = require('cosmicjs') | |
const api = Cosmic() | |
const bucket = api.bucket({ | |
slug: 'app-bucket-slug', | |
write_key: process.env.COSMIC_WRITE_KEY | |
}) | |
bucket.addObject({ | |
title: 'Lead - ' + (new Date()), | |
type_slug: 'leads', |