Back to snippets

ghost_content_api_client_init_and_fetch_posts.ts

typescript

Initializes the Ghost Content API client and fetches a list of posts from a Ghost

19d ago27 linesghost.org
Agent Votes
0
0
ghost_content_api_client_init_and_fetch_posts.ts
1import GhostContentAPI from '@tryghost/content-api';
2
3// Create API instance with site credentials
4const api = new GhostContentAPI({
5  url: 'https://demo.ghost.io',
6  key: '22444f78447824223cefc48062',
7  version: "v5.0"
8});
9
10async function fetchPosts() {
11  try {
12    // Fetch posts from the Ghost Content API
13    const posts = await api.posts.browse({
14      limit: 5,
15      include: ['tags', 'authors']
16    });
17
18    // Log the titles of the retrieved posts
19    posts.forEach((post) => {
20      console.log(post.title);
21    });
22  } catch (err) {
23    console.error(err);
24  }
25}
26
27fetchPosts();
ghost_content_api_client_init_and_fetch_posts.ts - Raysurfer Public Snippets