Contentful preview API hiccup

Before getting into any serious code, I’ve already experienced several hiccups so far. This most recent one involves setting up the site to use Contentful’s preview API in my local dev environment, which allows me to see draft content. After swapping out my access tokens, I could only get 401 errors.

In retrospect, I blame this hiccup solely on myself for neglecting to thoroughly read the manual. I have a tendency to only glance at documentation before diving in. In doing so, I only updated my accessToken to use the preview API token, but missed the part about specifying the host as, too. This totally makes sense, and I’m embarrassed I even reached out to support about this. For what it’s worth, I appreciate Stripe’s use of prefixes in their tokens to specify the environment (e.g., pk_test_), so a host option isn’t even necessary.

  1. I learned after the fact that the host option is necessary here because Contentful’s delivery API serves from the CDN while the preview API doesn’t.