Created
July 29, 2022 12:37
-
-
Save SimeonGriggs/2d07a03043d7f73d3bf675c4916279d4 to your computer and use it in GitHub Desktop.
Create a single `article` document as a draft and Schedule it to be published in 2025
This file contains 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
/** | |
* Create a single `article` document as a draft | |
* and Schedule it to be published in 2025 | |
* | |
* Save this file to the root of your Studio and run with: | |
* sanity exec createAndScheduleArticle.js --with-user-token | |
*/ | |
import sanityClient from 'part:@sanity/base/client' | |
import {uuid} from '@sanity/uuid' | |
const client = sanityClient.withConfig({apiVersion: `2021-05-19`}) | |
const {projectId, dataset} = client.config() | |
async function run() { | |
// Prepare the article along with a deterministic `drafts.` ID | |
const articleData = { | |
_id: `drafts.${uuid()}`, | |
_type: 'article', | |
title: 'Christmas 2025 Special', | |
} | |
await client | |
.create(articleData) | |
.then((doc) => { | |
// If document creation was successful, schedule its publishing | |
client | |
.request({ | |
method: 'POST', | |
uri: `/schedules/${projectId}/${dataset}`, | |
body: { | |
documents: [{documentId: doc._id}], | |
name: 'December 2025 release', | |
executeAt: '2025-12-25T19:45:00.000Z', | |
}, | |
}) | |
.then((schedule) => { | |
// Success! | |
console.log( | |
`Created draft and scheduled ${ | |
schedule.documents.length === 1 | |
? `1 document` | |
: `${schedule.documents.length} documents` | |
} for publishing` | |
) | |
}) | |
.catch((err) => console.error(err)) | |
}) | |
.catch((err) => console.error(err)) | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment