Jul 13, 2024 06:57 AM
How to access all WordPress posts using AirTable using REST API?
Jul 13, 2024 09:59 AM - edited Jul 15, 2024 10:25 PM
let inputConfig = input.config();
const WordPressAPI = (function () {
const baseUrl = inputConfig.wordpressBaseUrl;
const wordpressAPIKey = inputConfig.wordpressAPIKey;
const fetchPosts = async (page, perPage) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/posts`);
const params = { page, per_page: perPage }; // Both parameters are optional
Object.keys(params).forEach(key => params[key] !== undefined && url.searchParams.append(key, params[key]));
const response = await fetch(url, {
headers: { "Authorization": `Bearer ${wordpressAPIKey}` }
});
return response.json();
};
const fetchPostById = async (postId) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/posts/${postId}`);
const response = await fetch(url, {
headers: { "Authorization": `Bearer ${wordpressAPIKey}` }
});
return response.json();
};
const createPost = async (postData) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/posts`);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${wordpressAPIKey}`
},
body: JSON.stringify(postData)
});
return response.json();
};
const updatePost = async (postId, postData) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/posts/${postId}`);
const response = await fetch(url, {
method: "POST", // WordPress uses POST method for updating
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${wordpressAPIKey}`
},
body: JSON.stringify(postData)
});
return response.json();
};
const fetchCategories = async () => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/categories`);
const response = await fetch(url, {
headers: { "Authorization": `Bearer ${wordpressAPIKey}` }
});
return response.json();
};
const fetchCategoryById = async (categoryId) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/categories/${categoryId}`);
const response = await fetch(url, {
headers: { "Authorization": `Bearer ${wordpressAPIKey}` }
});
return response.json();
};
const createCategory = async (categoryData) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/categories`);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${wordpressAPIKey}`
},
body: JSON.stringify(categoryData)
});
return response.json();
};
const updateCategory = async (categoryId, categoryData) => {
const url = new URL(`${baseUrl}/wp-json/wp/v2/categories/${categoryId}`);
const response = await fetch(url, {
method: "POST", // WordPress uses POST method for updating
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${wordpressAPIKey}`
},
body: JSON.stringify(categoryData)
});
return response.json();
};
// Expose the functions
return {
fetchPosts,
fetchPostById,
createPost,
updatePost,
fetchCategories,
fetchCategoryById,
createCategory,
updateCategory
};
})();
// Make sure the IIFE is already executed and WordPressAPI is available
(async () => {
try {
const posts = await WordPressAPI.fetchPosts();
output.set('posts',posts);
} catch (error) {
console.error('Error fetching post:', error);
}
})();
Jul 13, 2024 04:09 PM
As I understand WordPress prepare values which will be read by AirTable and we place API connection inside AirTable settings? Is there any video how to do it?
Jul 15, 2024 10:27 PM - edited Jul 15, 2024 10:27 PM