Using an external news API to pre-populate post titles in Sanity Studio

19 replies
Last updated: May 19, 2020
Hello all. Im using a news headlines API. id like to pre-populate post titles with a call to the API. Any ideas ho I would do this? Many thanks
May 14, 2020, 11:43 AM
What's your usecase / scenario?
May 14, 2020, 11:49 AM
For each post template created from the news headlines API, I would like to then edit that post and add a gallery of images to illustrate the news headline. Does that make sense? Thanks
May 14, 2020, 11:54 AM
So whenever a new news item is available in the external API you want to create a post in Sanity that you can then populate with auxiliary data?
May 14, 2020, 12:01 PM
May 14, 2020, 12:23 PM
Sorry for the delay
May 14, 2020, 12:23 PM
Ok, then I would listen for new items in the news API (webhook, cron or something), and whenever there is a new item I would use one of our API clients ( ) to create a new draft. Here's an example with the JS client:
const doc = {
  _id: `drafts.${some-uuid}`,
  _type: 'news',
  title: newsItem.title

client.create(doc).then(res => {
  console.log('News item draft was created)
Then you would have a draft of the type
in Sanity that you can augment, and publish from the Sanity Studio when you are ready.
May 15, 2020, 7:51 AM
Thank you, its a little beyond me, but much appreciated
May 15, 2020, 2:08 PM
Do let us know if there's more we can assist with
May 15, 2020, 2:46 PM
Thank you I will try to implement your instructions and if (when๐Ÿ˜ƒ) I get stuck I will come back here
May 15, 2020, 3:23 PM
um... where does the code go?
May 15, 2020, 5:58 PM
Im using Gatsby BTW
May 15, 2020, 5:58 PM
Using your exmaple do I save for example news.js and import it in to schema.js?
May 15, 2020, 6:07 PM
Without knowing your hosting / server setup or anything, but typically something like this would need to run as a separate service. For example as a serverless function on Netlify or Vercel or similar.
Also the example above is only partial, so it would be more complex.

If you're not comfortable with all of that, perhaps it would be good idea to get some technical assistance to implement it for you.
May 15, 2020, 8:48 PM
Hi I think i am nearly there but have an error. Ive setup a file called test.js in the root of my studio directory
May 17, 2020, 11:39 AM
const { v4: uuidv4 } = require('uuid')
const sanityClient = require('@sanity/client')
const client = sanityClient({
	projectId: 'k0bc957l',
	dataset: 'production',
	useCdn: false,

function transformNewsFeed(externalFeed) {
	const article = {
		_type: 'article',
		_id: `news-id-${uuidv4()}`,
		name: externalFeed.title,

	return article

const flatten = require('lodash/flatten')
const fetch = require('node-fetch')

	.then((res) =&gt; res.json())
	.then((articles) =&gt;
	.then((data) =&gt; {
		return =&gt; client.createOrReplace(entry))
May 17, 2020, 11:39 AM
my schema.js is
// First, we must import the schema creator
import createSchema from 'part:@sanity/base/schema-creator'
import cats from './cats'
// Then import schema types from any plugins that might expose them
import schemaTypes from 'all:part:@sanity/base/schema-type'

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({
	// We name our schema
	name: 'default',

	// Then proceed to concatenate our document type
	// to the ones provided by any plugins that are installed
	types: schemaTypes.concat([
			type: 'document',
			name: 'article',
			title: 'Headline',
			fields: [
					name: 'name',
					type: 'string',
					name: 'author',
					type: 'string',
		/* Your types here! */
after running
node test.js
followed by
sanity start
I get a long list of console errors:

Module not found: Error: Can't resolve 'prop-types'
May 17, 2020, 11:43 AM
Any ideas? Thanks
May 17, 2020, 11:44 AM
The error
Error: Cannot find module 'prop-types'
Require stack:
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/base/lib/components/Document.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/require-uncached/index.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/server/lib/baseServer.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/server/lib/devServer.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/server/lib/server.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/core/lib/actions/start/startAction.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/util/lib/dynamicRequire.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/util/lib/lazyRequire.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/core/lib/commands/build/buildCommand.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/core/lib/commands/index.js
- /Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/core/lib/index.js
- /Users/jamesgrubb/.nvm/versions/node/v12.16.1/lib/node_modules/@sanity/cli/bin/sanity-cli.js
- /Users/jamesgrubb/.nvm/versions/node/v12.16.1/lib/node_modules/@sanity/cli/bin/sanity
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.&lt;anonymous&gt; (/Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/@sanity/base/lib/components/Document.js:8:41)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Module._compile (/Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Object.newLoader [as .js] (/Users/jamesgrubb/Dropbox/Dev/baremusic/studio/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
May 17, 2020, 11:54 AM
user N
, this slipped through the cracks. Did you figure it out?
May 19, 2020, 7:14 AM

Sanityโ€“ build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?