Skip to content

Instantly share code, notes, and snippets.

View tonyspiro's full-sized avatar
🚀
Building and shipping

Tony Spiro tonyspiro

🚀
Building and shipping
View GitHub Profile
@tonyspiro
tonyspiro / add-bucket.js
Created January 16, 2018 20:55
Cosmic JS NPM module - Add Bucket
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)
@tonyspiro
tonyspiro / get-objects.js
Last active January 17, 2018 16:02
Cosmic JS NPM module - Get Objects
const bucket = Cosmic.bucket({
slug: 'wedding-site',
read_key: ''
})
bucket.getObjects({
limit: 2
}).then(data => {
console.log(data)
}).catch(err => {
console.log(err)
@tonyspiro
tonyspiro / add-object.js
Created January 16, 2018 21:15
Cosmic JS NPM module - Add Object
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!"
},
@tonyspiro
tonyspiro / add-media.js
Created January 16, 2018 21:16
Cosmic JS NPM module - Add Media
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'
/*
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
})
@tonyspiro
tonyspiro / bucketSetupExample.js
Last active May 21, 2018 16:16
Cosmic JS - Bucket Setup Example
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'
})
# Install the Cosmic CLI
npm i -g cosmic-cli
cosmic login
# Node.js
cosmic init node-starter
# React
cosmic init react-starter
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) => {
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) {
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',