contentful-rest-api
Author Cloudapp
E.G.

Contentful Headless CMS — Managing Content via REST

September 19, 2024
Table of Contents

Contentful is a great Headless CMS that is easy to integrate with new or existing Next.js 14 projects. I use it a lot in my web projects. However, there are several ways to manage content (Creating, updating, deleting, and delivering content). Let’s do a deep dive into the REST capabilities of this CMS.

Below, you will find a list of all available Endpoints and some examples as well.

1. preview.contentful.com (Content Preview API)

  • Purpose

    This is the Preview API endpoint used to access unpublished (draft) content in Contentful. It’s useful for seeing changes in content before they go live.

  • Usage

    Typically used in development environments or for previewing content within content management systems (CMS).

  • Access

    Requires a preview API key.

  • Example Use Case

    A content editor wants to preview changes made to a blog post before it is published.

Let’s assume you have a Contentful Space with the ID “xxxyyy” and you are working on the “master” environment, and you have a content record with the ID “3xwgduhyZmdUYieeiW5Pq2”, then your URL would be:

https://preview.contentful.com/spaces/xxxyyy/environments/master/entries/3xwgduhyZmdUYieeiW5Pq2

Further information regarding the right use of Contentful Environments.

You can get the Content Preview Token from the Contentful Backend by clicking on “Settings” in the upper right corner, then “API Keys”. Create a new API Key or open the one present in the UI and scroll down till you see it. Now, create a GET Request in Postman and use the correct URL. Set the Preview Token as Bearer Token in the Authorization Tab and click “Send” to get the “preview” version of your content record.

As you can see in the JSON, the content entries type is “PageBlogPost”. Below the different fields used in the contenttype.

page-Blog-post-content-model
page-Blog-post-content-model

2. api.contentful.com (Management API)

  • Purpose

    This is the Content Management API endpoint used for managing content and other resources in a Contentful space. It provides CRUD operations (Create, Read, Update, Delete) for entries, assets, content types, environments, etc.

  • Usage

    Used by developers to automate tasks like content type creation, updating entries programmatically, or managing environment settings.

  • Access

    Requires a Content Management API key.

  • Example Use Case

    A developer writes a script to migrate content from one environment to another.

Let’s create a new content record of type “pageBlogPost”. The URL could be:

https://api.contentful.com/spaces/xxxyyy/environments/master/entries

Get the CMA Token from the “CMA tokens” section under settings in your Contentful account and use it again as Bearer Token in your Postman POST request. Add the headers “X-Contentful-Content-Type” -> pageBlogPost (or every content type you created) and “Content-Type” -> application/vnd.contentful.management.v1+json

Select “raw” for the body tab and use this example JSON.

As you can see I use two languages: German (DE) and English (EN). If the creation was successful you well receive a “201 Created” and you can see your record in the Contentful Backend.

Getting Data

With this URL -> https://api.contentful.com/spaces/xxxyyy/environments/master/entries/your-contentful-content-record-id

you can get the previously created content record JSON data.

Modifying Data

Let’s prepare a PUT request ->

https://api.contentful.com/spaces/xxxyyy/environments/master/entries/3xwgduhyZmdUYieeiW5Pq2

and the Header “X-Contentful-Version” to indicate the version for the modification. You can get the Version from the JSON of the previous GET request ( “publishedVersion”: 3, “publishedAt”: “2024–09–09T15:21:08.978Z”,)

3. cdn.contentful.com (Content Delivery API)

  • Purpose

    This is the Content Delivery API (CDA) endpoint used to deliver published content. It is backed by a Content Delivery Network (CDN) for efficient, fast access to content.

  • Usage

    Commonly used in production environments to fetch published content for websites or applications.

  • Access

    Requires a Content Delivery API key.

  • Example Use Case

    A website or mobile app fetches and displays live content for users.

Get the Content Delivery API — access token from your account under settings / API Keys

https://cdn.contentful.com/spaces/xxxyyy/environments/master/entries?content_type=pageBlogPost&limit=10

This call uses the fast Contentful CDN and gives you a list of 10 content records (type -> pageBlogPost)

4. graphql.contentful.com (GraphQL Content Delivery API)

  • Purpose

    This is the GraphQL Content Delivery API endpoint that provides content using the GraphQL query language, allowing more flexible and precise queries than the REST-based CDA.

  • Usage

    Suitable for applications that need fine-grained control over the content they fetch, reducing over-fetching or under-fetching of data.

  • Access

    Requires a Content Delivery API key.

  • Example Use Case

    A React-based application uses GraphQL queries to fetch specific fields from Contentful for rendering a component.

Use this Endpoint with the Content Delivery API — access token

https://graphql.contentful.com/content/v1/spaces/xxxyyy/environments/master

Create a Postman POST request, authorize with the Bearer Token (CDN API Key), and use this Graphql Query in the Body tab, selecting “GraphQL”. Use your content record ID.

Response JSON (Example)

5. images.ctfassets.net (Image API)

  • Purpose

    This endpoint is used to deliver and manipulate images stored in Contentful. It allows for image transformations such as resizing, cropping, and format conversion.

  • Usage

    Used in production to deliver optimized images for different devices or screen sizes.

  • Example Use Case

    A website uses this endpoint to fetch optimized, responsive images for faster loading times.

As you can see in the previous JSON response, Contentful uses this endpoint for image delivery and manipulation.

6. upload.contentful.com (Upload API)

  • Purpose

    This is the Upload API endpoint used to upload large files (assets) to Contentful. Unlike the Content Management API, which handles all types of resources, the Upload API is specifically optimized for large asset uploads.

  • Usage

    Commonly used in automation scripts or custom applications where large files need to be uploaded programmatically.

  • Access

    Requires a Content Management API key.

  • Example Use Case

    An application bulk uploads images or videos to be managed and published in Contentful

Conclusion:

  • preview.contentful.com

    Used for previewing unpublished content.

  • api.contentful.com

    Used for managing content and configurations.

  • cdn.contentful.com

    Used for delivering published content quickly.

  • graphql.contentful.com

    Used for delivering published content with flexible GraphQL queries.

  • images.ctfassets.net

    For delivering and transforming images.

  • upload.contentful.com

    For uploading large files as assets.

Each endpoint has a specific purpose and is optimized for different scenarios in the content management and delivery workflow.

Cloudapp-dev, and before you leave us

Thank you for reading until the end. Before you go:

Please consider clapping and following the writer! 👏 on our Medium Account

Or follow us on twitter -> Cloudapp.dev

Related articles