Getting started overview
Integrating with the Ghost CMS API involves setting up a Ghost instance, generating appropriate API keys, and then making requests to retrieve or manage content. Ghost provides two primary APIs: the Content API for fetching public data and the Admin API for content management operations like creating or updating posts. This guide focuses on the Content API for immediate content retrieval.
Before making your first API call, you will need access to a running Ghost instance, either through a Ghost Pro hosted plan or a self-hosted installation. The fundamental steps remain consistent across both deployment methods.
Quick Reference Guide
| Step | Action | Where to perform |
|---|---|---|
| 1. Set up Ghost | Deploy a Ghost instance (Ghost Pro or self-hosted) | Ghost Pro website or Ghost self-hosting documentation |
| 2. Generate API Key | Create an API integration to get a Content API Key | Ghost Admin Dashboard > Integrations |
| 3. Identify API URL | Note your Ghost site's API URL | Ghost Admin Dashboard > Integrations > Your API Integration |
| 4. Construct Request | Formulate your first API call to fetch posts | Your preferred HTTP client or code editor |
| 5. Execute Request | Send the request and verify the response | Your preferred HTTP client or code environment |
Create an account and get keys
To interact with the Ghost API, you need access to a Ghost instance and a valid API key. If you don't have a Ghost instance running, you can either sign up for a Ghost Pro plan or install Ghost locally or on a server. Once your Ghost instance is operational, follow these steps to obtain your API key:
- Log in to Ghost Admin: Access your Ghost site's admin panel, typically found at
yourdomain.com/ghost. - Navigate to Integrations: In the left-hand sidebar, click on 'Settings' (the gear icon), then select 'Integrations'.
- Add a new integration: Click the '+ Add custom integration' button.
- Name your integration: Provide a descriptive name for your API integration, for example, 'My Frontend App' or 'Data Exporter'. This name helps you identify the purpose of the key later.
- Retrieve API Key and API URL: After naming, Ghost will generate a Content API Key and display your API URL (also known as the API Host or API Endpoint). Copy both the 'Content API Key' and the 'API URL'. These are essential for making API requests. The API URL will generally follow the format
https://yourdomain.com/ghost/api/content/. Keep these credentials secure, especially if you also generate an Admin API key later.
The Content API Key is designed for read-only access to public content. If you need to perform write operations (e.g., creating posts or members), you would generate an Admin API Key following a similar process after enabling it in your Ghost instance settings, as detailed in the Ghost Admin API documentation.
Your first request
With your API Key and API URL, you can now make your first request to fetch content. This example demonstrates fetching a list of posts from your Ghost instance. We'll use a simple HTTP GET request.
Request Details
- Method: GET
- Endpoint:
YOUR_API_URL/posts/ - Query Parameters:
key: Your Content API Keyfields: Optional. Specify fields to retrieve, e.g.,title,slug,feature_imagelimit: Optional. Number of posts to return, e.g.,5
Example using curl
Replace YOUR_API_URL and YOUR_CONTENT_API_KEY with the values you obtained from your Ghost Admin panel.
curl "YOUR_API_URL/posts/?key=YOUR_CONTENT_API_KEY&limit=3&fields=title,slug,published_at"
A successful response will return a JSON object containing an array of post objects:
{
"posts": [
{
"title": "My First Post",
"slug": "my-first-post",
"published_at": "2023-10-27T10:00:00.000Z"
},
{
"title": "Another Great Article",
"slug": "another-great-article",
"published_at": "2023-10-26T14:30:00.000Z"
},
{
"title": "Hello World",
"slug": "hello-world",
"published_at": "2023-10-25T08:15:00.000Z"
}
],
"meta": {
"pagination": {
"page": 1,
"limit": 3,
"pages": 1,
"total": 3,
"next": null,
"prev": null
}
}
}
Example using Node.js with fetch
Ghost provides an official Node.js Content API client, but a raw fetch request works directly:
const GHOST_API_URL = 'YOUR_API_URL';
const GHOST_CONTENT_API_KEY = 'YOUR_CONTENT_API_KEY';
async function getGhostPosts() {
try {
const response = await fetch(
`${GHOST_API_URL}/posts/?key=${GHOST_CONTENT_API_KEY}&limit=5&fields=title,slug,excerpt}`
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data.posts);
} catch (error) {
console.error('Error fetching Ghost posts:', error);
}
}
getGhostPosts();
This will log an array of post objects to your console, similar to the curl output.
JSONP Callback (Optional)
For cross-domain requests in older browser environments that don't support CORS, Ghost's Content API supports JSONP. You can add a &callback=yourFunctionName parameter to your request URL, and the API will wrap the JSON response in a function call. However, modern development typically relies on CORS or server-side fetching, as discussed in Mozilla's Cross-Origin Resource Sharing documentation.
Common next steps
After successfully making your first API call, consider these next steps to further integrate with the Ghost CMS API:
- Explore Content API Endpoints: Beyond posts, the Content API offers endpoints for pages, authors, tags, and settings. Refer to the Ghost Content API reference to understand all available resources and their parameters.
- Implement Admin API: If your application requires managing content (creating, updating, deleting posts or users), you will need to use the Admin API. This involves generating an Admin API Key and understanding the authentication mechanism, which includes JWTs (JSON Web Tokens). Details are available in the Ghost Admin API documentation.
- Utilize SDKs: For Node.js applications, consider using the official Ghost Content API JavaScript SDK and Admin API JavaScript SDK. These SDKs handle authentication, request formatting, and error handling, streamlining development.
- Build a Frontend Application: Use a static site generator (like Next.js, Gatsby, or Hugo) or a single-page application (SPA) framework (React, Vue, Angular) to fetch content from Ghost and render it dynamically.
- Webhooks: Set up webhooks in Ghost to receive real-time notifications when content changes (e.g., a new post is published). This can trigger rebuilds of static sites or cache invalidation. Configure webhooks in the Ghost Admin under Settings > Integrations.
- Authentication for Members: If your Ghost site utilizes memberships, explore the Members API for managing member data and authentication, detailed in the Ghost Members API documentation.
- Error Handling: Implement robust error handling in your API calls to gracefully manage network issues, invalid API keys, and other potential problems. Review the Ghost API error codes.
Troubleshooting the first call
If your first API request to Ghost does not return the expected data, consider these common issues:
- Incorrect API Key: Double-check that you are using the correct Content API Key. The key is a long alphanumeric string. Copy it directly from the Ghost Admin Integrations page to avoid typos.
- Incorrect API URL: Ensure your API URL is accurate. It should typically be
https://yourdomain.com/ghost/api/content/. Verify that there are no trailing slashes or other formatting errors. - Firewall or Network Issues: If you are self-hosting Ghost, ensure that your server's firewall or network configuration allows incoming requests to the Ghost API port (usually 443 for HTTPS).
- Content Visibility: The Content API only returns published posts and pages. Drafts or private content will not appear. Ensure you have published at least one post and set its visibility to 'Public' within the Ghost Admin.
- Missing Content: If your Ghost instance is new, you might not have any content yet. Create a test post in the Ghost Admin panel before making your API request.
- CORS Issues (Browser-based requests): If you're making API calls directly from a browser-based application (e.g., JavaScript in a web page), you might encounter Cross-Origin Resource Sharing (CORS) errors. Ghost's Content API is generally configured to allow CORS, but if you're self-hosting, ensure your Nginx or Apache configuration isn't blocking it. For local development, browser extensions or proxy servers can sometimes bypass this temporarily, but a proper solution involves configuring the server or using server-side fetching.
- Rate Limiting: While unlikely for a first request, be aware that Ghost's API has rate limits to prevent abuse. If you make many rapid requests, you might temporarily be blocked. Check the HTTP response headers for
X-RateLimit-Limit,X-RateLimit-Remaining, andX-RateLimit-Reset, as specified in the Ghost API rate limiting documentation. - API Version Mismatch: Ensure your API requests are compatible with the Ghost version running. The API URL typically includes a version segment (e.g.,
v5), but for the Content API, the structure is generally stable. - Server Logs: For self-hosted instances, check your Ghost server logs for more detailed error messages. These logs can provide insights into what might be going wrong on the backend.