Generate an RSS feed from HTTP POST requests, retrieve via GET request
@dylburger
code:
data:privatelast updated:1 year ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
rss_channel_metadata
auth
to use OAuth tokens and API keys in code via theauths object
params
Feed Title

The name of the channel. It's how people refer to your service. If you have an HTML website that contains the same information as your RSS file, the title of your channel should be the same as the title of your website.

 
string ·params.title
Feed Description

Phrase or sentence describing the channel.

 
string ·params.description
Feed Link

The URL to the HTML website corresponding to the channel.

 
string ·params.link
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
}
42
const get = require("lodash.get") 
const isEqual = require("lodash.isequal")

// Parse hostname from URL to generate feedLinks below
// You can make GET requests to /rss, /json, or /atom
// to retrieve these feeds
const hostname = new URL(event.url).hostname
this.rssFeedLink = `https://${hostname}/rss`
this.jsonFeedLink = `https://${hostname}/json`
this.atomFeedLink = `https://${hostname}/atom`

// We only add the required options here, but you can add
// any of the options from https://www.npmjs.com/package/feed
// See https://validator.w3.org/feed/docs/rss2.html for a
// full description of each element
const { title, description, link } = params

const feedOptions = {
  title,
  description,
  link,
  id: link,
  feedLinks: {
    rss: this.rssFeedLink,
    json: this.jsonFeedLink,
    atom: this.atomFeedLink, 
  },
}

// If feedOptions has changed (e.g. if you changed Feed Title), 
// save to workflow state.
const savedFeedOptions = get($checkpoint, "feedOptions")
if (!isEqual(savedFeedOptions, feedOptions)) {
  // If this is our first time running the workflow,
  // $checkpoint will be undefined. Instantiate it to be an object
  if (!$checkpoint) $checkpoint = {}
  $checkpoint.feedOptions = feedOptions
}

this.feedOptions = feedOptions
steps.
add_new_item
auth
to use OAuth tokens and API keys in code via theauths object
params
Secret

The API key you'd like to require on inbound POST requests

 
string ·params.secret
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
}
38
const get = require("lodash.get")

// Send an HTTP POST request to add a new item to the feed
// From the RSS spec (https://validator.w3.org/feed/docs/rss2.html):
// "All elements of an item are optional, however at least one of title or description must be present."

// The body of the request must match the properties the feed package 
// expects in the addItem method: https://www.npmjs.com/package/feed
if (event.method === "POST") {
  // Require secret is present on requests to add items to the feed
  if (params.secret !== event.headers.secret) {
    $end("Invalid secret")
  }

  // Get any items stored in state
  // This keeps the last MAX_ITEMS items POSTed to this feed
  MAX_ITEMS = 15
  
  const items = get($checkpoint, "items", [])
  if (items.length > MAX_ITEMS) {
    items.shift()
  }
  items.push(event.body)

  if (!$checkpoint) $checkpoint = {}
  $checkpoint.items = items

  $respond({
    body: {
      success: true,
      msg: "Saved new item to feed"
    }
  })
  
  $end("Saved new item to feed")
}
steps.
get_feeds
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
}
40
const Feed = require('feed').Feed; 
const get = require("lodash.get")

// Build feed object from channel metadata generated above
const feed = new Feed(steps.rss_channel_metadata.feedOptions)

const items = get($checkpoint, "items", [])
for (const item of items) {
  feed.addItem(item)
}

// Did the user issue a GET request to retrieve feed data?
if (event.method === "GET") {
  this.pathname = new URL(event.url).pathname
  let body
  switch (this.pathname) {
    case '/rss':
      body = feed.rss2()
      break
    case '/json':
      body = feed.json1()
      break
    case '/atom':
      body = feed.atom1()
      break
    default:
      body = feed.rss2()
  }

  $respond({
    headers: {
      "Content-Type": "application/xml",
    },
    body,
  })

  $end(`Retrieved ${this.pathname} feed`)
}