James Valentine
James Valentine

Reputation: 103

Friendlier way to Retrieve Content Types with Contentful

I'm evaluating Contentful.com as the Content Engine for an Angular SPA.

The issue I'm facing is with retrieving entries by content type (e.g. get all entries of type "blog"). As described in the documentation example, this is done as follows:

/spaces/spaceid/entries?access_token=token&content_type=cat

So in my case rather than "cat", content_type="blog" or content_type="news" would be how I'd expect to interact.

The reality I'm facing is that retrieving things via content_type doesn't let me use the name of my content type - it expects the ID which is an ugly GUID, so my queries look like this:

/spaces/spaceid/entries?access_token=token&content_type=2wKn6yEnZewu2SCCkus4as

The troubles with this is:

The only solution I can see at present is to pre-retrieve a content-type to id mapping on page load and use that - but it's not going to be so great for performance.

The documentation appears to always use friendly readable content-type ids (e.g. cat) but this isn't my experience.

So, to solve this, is there:

  1. a way to set content-types to have friendly ids? or
  2. a way to retrieve the entry by name using a different query parameter?

Upvotes: 5

Views: 4042

Answers (4)

Joshua Tuscan
Joshua Tuscan

Reputation: 373

This is no longer the case, you can retrieve all entries of a certain content type with the following type of query:

options = {
    include: 10,
    limit: 1000,
    order: "fields.publishDate",
    "fields.tags[in]": title_case_filter,
    content_type: "blog"
  };

In this case "blog" is the contentType id assigned when creating the content type.

Upvotes: 0

wprater
wprater

Reputation: 1032

You can actually use the SDK to create a contentType with a specified id. You must use the createContentTypeWithId method of the ContentfulSpaceAPI.

It's signature looks like

* @property {function(id: string, data: {name: string, fields: Array}): Promise<ContentType>} createContentTypeWithId - creates a ContentType with a specified id

Upvotes: 0

aw04
aw04

Reputation: 11177

If you visit the page for the content type in the web app, check the end of the url. It should look something like:

content_types/yourContentType

In this case, yourContentType is the id you can use (this is what you see in the documentation). It took me a while to find this in the docs, could definitely be more obvious.

Upvotes: 0

sdepold
sdepold

Reputation: 6241

I used to have the same question and basically run for the pre-retrieval of content-types followed by the creation of a tiny map that would translate the name to the if. The performance is not too bad as one is doing that only once at the startup of the app. Also Contentful uses a CDN which makes the response almost instant.

Anyways, here is the good news: Although you cannot define the IDs of the content types via the User Interface of Contentful, you can totally do that via their API like this via curl (you can also use the various SDKs):

curl -H 'Authorization: Bearer <access-token>' \ 
  -X PUT \
  -d '{"name": "Blog", "fields": [{"name":"Name","id":"name","type":"Symbol"}]}' \
  https://api.contentful.com/spaces/<spaceId>/content_types/blog

This will create a content type with the id "blog" which has a field "name". Once you created a content type like this you can also just go to the User Interface and add more fields to it.

Upvotes: 3

Related Questions