🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro JSON Feed: Add feed.json to Your Blog

Add an Astro JSON Feed to your blog: a feed.json endpoint built from your content collection, JSON Feed 1.1 fields, autodiscovery and optional full content.

#Astro#JSON Feed#RSS#Content Collections

To add a JSON Feed to an Astro blog, create a static endpoint at src/pages/feed.json.ts that maps your content collection to JSON Feed 1.1 items and returns them with Response.json(). Astro writes feed.json at build time, and a <link rel="alternate" type="application/feed+json"> tag lets readers find it. No package is needed.

Key Takeaways

  • JSON Feed is a simple, documented alternative to RSS that many feed readers support.
  • A static endpoint is about 30 lines; Astro builds it to dist/feed.json.
  • Required fields: version, title and items, with an id per item.
  • Offer it alongside RSS, not instead of it, since RSS has wider support.

Create the endpoint

// src/pages/feed.json.ts
import type { APIRoute } from 'astro';
import { getCollection } from 'astro:content';

export const GET: APIRoute = async ({ site }) => {
  const base = new URL('/', site);
  const posts = (await getCollection('blog', ({ data }) => !data.draft))
    .sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
    .slice(0, 30);

  const feed = {
    version: 'https://jsonfeed.org/version/1.1',
    title: 'Example Blog',
    home_page_url: base.href,
    feed_url: new URL('feed.json', base).href,
    description: 'Articles about building fast sites.',
    language: 'en',
    authors: [{ name: 'Example Author', url: base.href }],
    items: posts.map((post) => {
      const url = new URL(`blog/${post.id}/`, base).href;
      return {
        id: url,
        url,
        title: post.data.title,
        summary: post.data.description,
        content_text: post.data.description,
        date_published: post.data.pubDate.toISOString(),
        date_modified: (post.data.updatedDate ?? post.data.pubDate).toISOString(),
        tags: post.data.tags ?? [],
      };
    }),
  };

  return Response.json(feed, {
    headers: { 'Content-Type': 'application/feed+json; charset=utf-8' },
  });
};

site comes from the site option in astro.config.mjs, so set it first; feed URLs must be absolute. On a static build, the endpoint runs once and its output is saved as dist/feed.json. The Astro API endpoints guide explains how static endpoints work.

Adjust the field names to your collection schema. The example assumes title, description, pubDate and optional updatedDate, tags and draft, as in a typical Content Collections setup.

The fields that matter

Field Required Notes
version Yes https://jsonfeed.org/version/1.1
title Yes Feed name
items[].id Yes Unique and stable; the post URL works well
items[].content_html or content_text One of them Full post or a summary
home_page_url, feed_url Recommended Absolute URLs
date_published Recommended RFC 3339, which toISOString() produces
image, banner_image Optional Absolute URLs to images

Keep id stable forever. If you change URLs later, keep the old IDs, otherwise every reader shows all your posts as new.

Full content in the feed

content_text with the description gives readers a summary. For full articles, put the rendered HTML in content_html. The rendering loop from the full-content RSS tip works unchanged: render each entry with the Container API, make URLs absolute and sanitize it, then assign the result to content_html instead of content.

Add autodiscovery

In your base layout’s <head>, list both feeds:

---
// src/layouts/BaseLayout.astro (head excerpt)
---
<link rel="alternate" type="application/rss+xml" title="Example Blog (RSS)" href={new URL('rss.xml', Astro.site)} />
<link rel="alternate" type="application/feed+json" title="Example Blog (JSON Feed)" href={new URL('feed.json', Astro.site)} />

Readers that support JSON Feed pick it; others fall back to RSS. Validate the output with the validator linked from jsonfeed.org before sharing the URL. If you haven’t set up RSS yet, start with the Astro RSS feed guide. Many blog themes already ship an RSS endpoint you can copy this next to.

FAQ

What is a JSON Feed?

JSON Feed is a syndication format like RSS or Atom, written in JSON instead of XML. It was published in 2017 and its current version is 1.1. Many feed readers support it alongside RSS.

Do I need a package to create a JSON Feed in Astro?

No. A static endpoint that builds an object and returns Response.json() is enough. @astrojs/rss only produces RSS XML.

Should I replace RSS with JSON Feed?

No. Keep RSS, which every feed reader supports, and add JSON Feed as a second option. Both can be generated from the same collection query.