Search code examples
javascriptcontentful

Get Contentful entry by ID and locale


The Contentful npm package provide access to all the functionality from the API. In my case, I know the ID for the entry I want, but need to retrieve the data for a non-default locale and I can't see any way to pass the locale option. My query looks like this:

const { createClient } = require('contentful');

const contentfulClient = createClient({
  accessToken: 'xxxxxxxx',
  space: 'xxxxxxx',
});

const entry = contentfulClient
  .getEntry('xxxxxx')
  .catch(console.error);

I know I could do the following:

const data = await contentfulClient
  .getEntries({
    content_type: 'xxxxxx'
    locale: 'cy',
    'sys.id': xxxxx,
  })
  .catch(console.error);

const [entry] = data.items;

But this requires the content type and returns an array of entries, which seems counter intuitive when I know the exact entry I want. Am I missing something? Seems like a logical thing to expect it to do.


Solution

  • It doesn't say so on the API documentation but you can absolutely use the locale= parameter via the API.

    ▶ curl -H "Authorization: Bearer $CONTENTFUL_ACCESS_TOKEN" https://cdn.contentful.com/spaces/$CONTENTFUL_SPACE_ID/entries/6wU8cSKG9UOE0sIy8Sk20G
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "xxxx"
          }
        },
        "id": "6wU8cSKG9UOE0sIy8Sk20G",
        "type": "Entry",
        "createdAt": "2018-09-06T22:01:55.103Z",
        "updatedAt": "2018-10-08T19:26:59.382Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 14,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "section"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "internalTitle": "test test test",
        ...
    
    ▶ curl -H "Authorization: Bearer $CONTENTFUL_ACCESS_TOKEN" https://cdn.contentful.com/spaces/$CONTENTFUL_SPACE_ID/entries/6wU8cSKG9UOE0sIy8Sk20G\?locale\=\*
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "xxxx"
          }
        },
        "id": "6wU8cSKG9UOE0sIy8Sk20G",
        "type": "Entry",
        "createdAt": "2018-09-06T22:01:55.103Z",
        "updatedAt": "2018-10-08T19:26:59.382Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 14,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "section"
          }
        }
      },
      "fields": {
        "internalTitle": {
          "en-US": "test test test"
        },
        ...
    
    

    The documentation for the contentful JS client says:

    Parameters:
    Name Type Attributes Description. id string
    query Object optional.
    Object with search parameters. In this method it's only useful for locale.

    so you'd add the locale as the second parameter to getEntry like so:

    const entry = contentfulClient
      .getEntry('xxxxxx', { locale: 'en-US' })