Back

Step by Step Guide to Building an Adobe Experience Manager API Integration in JS

Aug 3, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Adobe Experience Manager (AEM) API integration? You're in for a treat. AEM's API is a powerful tool that'll let you interact with your AEM instance programmatically. Whether you're looking to manage content, handle assets, or automate workflows, this guide will get you up and running in no time.

Prerequisites

Before we jump in, make sure you've got:

  • An AEM instance ready to go
  • Node.js and npm installed on your machine
  • A solid grasp of JavaScript and RESTful APIs

Got all that? Great! Let's get our hands dirty.

Setting up the project

First things first, let's set up our project:

mkdir aem-api-integration cd aem-api-integration npm init -y npm install axios dotenv

We're using axios for HTTP requests and dotenv to manage our environment variables. Trust me, your future self will thank you for using dotenv.

Authentication

AEM uses OAuth 2.0 for authentication. Here's how to get started:

  1. Get your API credentials from the AEM instance.
  2. Create a .env file in your project root:
AEM_HOST=https://your-aem-instance.com
AEM_CLIENT_ID=your_client_id
AEM_CLIENT_SECRET=your_client_secret

Now, let's set up authentication:

require('dotenv').config(); const axios = require('axios'); async function getAccessToken() { const response = await axios.post(`${process.env.AEM_HOST}/oauth/token`, { grant_type: 'client_credentials', client_id: process.env.AEM_CLIENT_ID, client_secret: process.env.AEM_CLIENT_SECRET }); return response.data.access_token; }

Making API requests

With our access token in hand, let's make a basic GET request:

async function getContent(path) { const token = await getAccessToken(); const response = await axios.get(`${process.env.AEM_HOST}${path}`, { headers: { Authorization: `Bearer ${token}` } }); return response.data; }

CRUD operations

Now for the fun part - let's create, read, update, and delete content:

async function createContent(path, data) { const token = await getAccessToken(); return axios.post(`${process.env.AEM_HOST}${path}`, data, { headers: { Authorization: `Bearer ${token}` } }); } async function updateContent(path, data) { const token = await getAccessToken(); return axios.put(`${process.env.AEM_HOST}${path}`, data, { headers: { Authorization: `Bearer ${token}` } }); } async function deleteContent(path) { const token = await getAccessToken(); return axios.delete(`${process.env.AEM_HOST}${path}`, { headers: { Authorization: `Bearer ${token}` } }); }

Advanced features

Want to query content or work with assets? Here's a taste:

async function queryContent(query) { const token = await getAccessToken(); return axios.post(`${process.env.AEM_HOST}/bin/querybuilder.json`, query, { headers: { Authorization: `Bearer ${token}` } }); } async function uploadAsset(path, file) { const token = await getAccessToken(); const formData = new FormData(); formData.append('file', file); return axios.post(`${process.env.AEM_HOST}${path}`, formData, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'multipart/form-data' } }); }

Error handling and best practices

Always wrap your API calls in try/catch blocks and handle rate limiting:

try { const content = await getContent('/content/mysite/en'); console.log(content); } catch (error) { if (error.response && error.response.status === 429) { console.log('Rate limit exceeded. Try again later.'); } else { console.error('An error occurred:', error.message); } }

Testing the integration

Don't forget to test! Here's a simple example using Jest:

test('getContent returns data', async () => { const content = await getContent('/content/mysite/en'); expect(content).toBeDefined(); expect(content.jcr:primaryType).toBe('cq:Page'); });

Conclusion

And there you have it! You're now equipped to build robust AEM API integrations. Remember, this is just the tip of the iceberg. AEM's API is vast and powerful, so don't be afraid to explore and experiment.

For more in-depth information, check out the official AEM API documentation. Happy coding!