Help

Re: How to access all posts using AirTable using REST API?

2887 1
cancel
Showing results for 
Search instead for 
Did you mean: 
NEF1QLSZ4U
6 - Interface Innovator
6 - Interface Innovator

How to access all WordPress posts using AirTable using REST API?

3 Replies 3
JordanTLewis514
4 - Data Explorer
4 - Data Explorer

 

 

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);
  }
})();

 

 

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?

Just follow these screenshots and copy and paste the script that I added into the scripting automations, should be super simple. 

tbh I dont have a wordpress account so I can't really test it out