Contentful logo

Contentful Community

'Contentful is not defined' error when using CMA

Hi,

I’m working on connecting Contentful to an app I’m building in PlayCanvas (using javascript).

I’ve got everything working perfectly using the Delivery API. I’m having trouble getting it to connect to the Management API though.

In PlayCanvas there’s a section to include links to external script files. I’m using the following links from the installation instruction on GitHub:

Delivery
https://cdn.jsdelivr.net/npm/contentful@latest/dist/contentful.browser.min.js

Management
https://cdn.jsdelivr.net/npm/contentful-management@latest/dist/contentful-management.browser.min.js

It feels like having both scripts included is causing some conflicts so I stripped out everything related to content delivery as a test.

Using only the following code and the gives me an error:
Uncaught ReferenceError: contentful is not defined

this.cmaClient = contentful.createClient({
   accessToken: this.ManagementToken 
});

‘this.ManagementToken’ is a variable set up using the personal access token I generated in the contentful web app. It makes no difference if I paste the token in directly.

This code works with the CDA, but not the CMA. What am I doing wrong?

Best practices question - My app both receives data and updates entries (never creating, just updating) should I use the CMA for everything, or CDA when receiving and CMA when sending. Hopefully that question makes sense.

Thank you!

Hi @ripcorddev! Welcome!

If you are using the jsdelivr.net URL to include the contentful-management.js package in your webpage, then you would need to access it as contentfulManagement in your code. Like so:

this.cmaClient = contentfulManagement.createClient({
   accessToken: this.ManagementToken 
});

See the Browser section of the contentful-management.js GitHub page for details.

As to your best practices question, it’d be best to just use the CMA for everything.

Hope this helps!

Thanks @charlie, that solved my issue! Everything is running perfectly now.

I was following the example in the Content Management API link a little further up on the GitHub page. I guess I should have read further down before clicking the link. :smiley:

Thank you so much!