Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friendlier way to Retrieve Content Types with Contentful

Tags:

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:

  • not developer friendly
  • not portable across environments (i.e. a test Space and a production space)

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?
like image 645
James Valentine Avatar asked Oct 19 '22 03:10

James Valentine


1 Answers

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.

like image 123
sdepold Avatar answered Oct 22 '22 22:10

sdepold