Skip to content

Schedule a week of posts (Node.js)

Node.js 18 or later, no packages. It schedules next week, Monday to Sunday: one post a day at 9:00 in Oslo, to every Channel that can take one.

week.mjs
const API = 'https://app.pubrivo.com/api'
const headers = {
Authorization: `Bearer ${process.env.PUBRIVO_KEY}`,
'Content-Type': 'application/json',
}
const posts = [
'Monday: sourdough, all day.',
'Tuesday: cinnamon knots from 8.',
'Wednesday: rye with sunflower seeds.',
'Thursday: cardamom buns are back.',
'Friday: croissants, until they run out.',
'Saturday: the big tray. Come early.',
'Sunday: closed. See you Monday.',
]
const channels = await fetch(`${API}/channels`, { headers }).then((r) => r.json())
const channelIds = channels.data.filter((c) => c.canPostNow).map((c) => c.id)
// Today in Oslo, as midnight UTC, so adding days never trips over a clock change.
const today = new Date(new Date().toLocaleDateString('en-CA', { timeZone: 'Europe/Oslo' }))
const nextMonday = (8 - today.getUTCDay()) % 7 || 7
// 9:00 in Oslo, a number of days from today, as an instant.
function nineInOslo(daysAhead) {
const day = new Date(today.getTime() + daysAhead * 86_400_000).toISOString().slice(0, 10)
const guess = new Date(`${day}T09:00:00Z`)
const oslo = new Date(guess.toLocaleString('en-US', { timeZone: 'Europe/Oslo' }))
const utc = new Date(guess.toLocaleString('en-US', { timeZone: 'UTC' }))
return new Date(guess.getTime() - (oslo - utc)).toISOString()
}
for (const [i, text] of posts.entries()) {
const response = await fetch(`${API}/posts`, {
method: 'POST',
headers,
body: JSON.stringify({ text, channelIds, scheduledAt: nineInOslo(nextMonday + i) }),
})
const body = await response.json()
if (!response.ok) {
console.error(text, body.error.message, body.error.blockers ?? '')
continue
}
console.log(body.data.at, text)
}
Terminal window
PUBRIVO_KEY=pbv_… node week.mjs

A post Instagram cannot take — it needs an image — comes back with a blocker naming it, and the rest still schedule. Aim at fewer Channels, or add media.