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.
Before we jump in, make sure you've got:
Got all that? Great! Let's get our hands dirty.
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
.
AEM uses OAuth 2.0 for authentication. Here's how to get started:
.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; }
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; }
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}` } }); }
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' } }); }
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); } }
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'); });
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!