How to fetch newsletters list to make a newsletter archive

Hello :wave: Thank you for the amazing work on Ghost CMS!

We use Ghost Pro CMS for our live site. Recently I was asked to add a newsletter archive - a page that displays only newsletters that have been sent. I worked on this locally on the docker image ghost:5.74.0-alphine (newest). Unfortunately it seems that the API (I mean handlebars templates) doesn’t give us a possibility to show them. Only published posts are available.
I was reverse engineering how does the admin work in order to show them and it seems like it’s using http://localhost:2368/ghost/api/admin/posts/?formats=mobiledoc%2Clexical&limit=30&page=1&filter=status%3A%5Bdraft%2Cscheduled%2Cpublished%2Csent%5D endpoint.

Then I tried

const response = await adminApi.posts.browse({ limit: 999, filter: 'status:sent' });

Partial response:

"email_segment": "all", "url": "http://localhost:2368/email/40f39af0-b8e5-464b-b77a-7f63ca1bdaba/", "excerpt": "Test newsletter 3",

And I was able to fetch them, but unfortunately I’m not sure how to use the SDK without exposing admin API key publicly. Is there another way to fetch sent newsletters? We can’t publish & sent them, as then they will be available on our global landing page

You’re right that you do NOT want to run that code client-side. If you had to run that code, you’d have to have a separate cloud function (that knew the API key) run that code and serve the result to the browser.

But there’s an easier way. You CAN publish and send them, and just edit the routing file to not have them show on your global landing page. For /many/ themes that just means loading a different version of routes.yaml. For other themes, that may mean some easy changes to the theme files, if the landing page is using a #get request instead.

Check out the section on ‘filtering’, in particular.

As Cathy suggests, use routing. Extremely powerful, once you get your head around it. Quirk: it is not included with the theme, has to be uploaded & managed separately.

You have routes, collections, filters, templates and controllers at your disposal.
The newsletter archive is a few lines in routes.yaml like this

    permalink: /email/{slug}/
    template: archive
    filter: tag:hash-newsletter    
    permalink: /{slug}/
    template: index
    filter: tag:-hash-newsletter

Posts tagged with #newsletter show up in the newsletter archive, the filter for index removes them from the homepage.

1 Like