Calling the Content Fetch API using the JavaScript SDK

Follow

While you can use the Content Fetch REST API using any programming language or framework, we've provided an official JavaScript SDK so you can integrate Agility CMS into your JavaScript project quickly and seamlessly.

View the SDK Docs

Check it out on GitHub

View on NPM

Support

The JavaScript SDK can be used within the browser (using NPM or a traditional direct script reference) and it can also be used in server-side node.js apps.

Using the SDK with NPM (Node Package Manager)

  1. Install the Agility Content Fetch JS SDK package into your project:
    > npm install @agility/content-fetch
  2. Import the SDK when you need to use it in your JavaScript file(s):
    import agility from '@agility/content-fetch'
  3. Initialize the client with your Guid, ApiKey, and optionally set isPreview=true (if you are using a preview API Key).

    Don't know your Guid or API Key? See Retrieving your API Key(s), Guid, and API URL.

    Then you can use the client to execute requests in a variety of ways:

Using the SDK with a Script Reference and Global Variable

While we highly recommend using an NPM set up for new projects, we understand that there are times where that just isn't feasible. For these cases, you can include the SDK by referencing it in a <script> tag and then accessing it using the assigned global variable called agility.

  1. Add a reference to the agility-content-fetch.browser.js file:

    <!-- Use a specific version (i.e. 0.5.0) --> 
    <script type="text/javascript" src="https://unpkg.com/@agility/content-fetch@0.5.0/dist/agility-content-fetch.browser.js"></script>

    <!-- Or, Use the latest version -->
    <script type="text/javascript" src="https://unpkg.com/@agility/content-fetch@latest/dist/agility-content-fetch.browser.js"></script>
  2. Next, you can access the global variable named agility, initialize the client, and start making API calls.

 

See the Pen Vanilla JS AgilityCMS Content Fetch SDK Usage by James Vidler (@jamesvidler-the-selector) on CodePen.

 

1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.