The API to Posts plugin allows you to setup multiple endpoints and create custom posts or WooCommerce products from these multiple endpoints.
When we talk about ‘multiple endpoints’, there are a few different scenarios that need to be considered and you should follow the one that matches your situation.
Keep in mind that there are thousand and thousands of APIs available and all of them are setup and structured differently, so careful consideration needs to be made here.
Please check out the article on how to get started with the API to Posts plugin.
Let’s look at the different scenarios that the API to Posts plugin supports.
Multiple Endpoints – Unrelated Products
In this scenario, you might have an endpoint that imports products from Supplier A and then you have another endpoint that imports products from Supplier B. The products from each supplier are unrelated and you want to create WooCommerce products from every product within both endpoints.
In this case, the endpoints are unrelated so nothing special is required in the setup. You simply set up both endpoints and run the importer and creator for each endpoint.
Multiple Endpoints – Same Products
Within this scenario, we are using multiple endpoints but the endpoints are returning data for the same products, albeit different data from each endpoint.
Here, there are two paths that we can follow.
1 – All endpoints contain list of items
In this scenario, you might have an API with a ‘main’ endpoint that contains a list of all products and includes limited information such as just the name of each product, a description of the product and an ID. The API also has a ‘prices’ endpoint that contains the same full list of products but only contains pricing information and an ID. There is also an ‘images’ endpoint that contains all images and an ID for each product.
When setting up these multiple endpoints, the Unique Item Key field must contain exactly the same value.
You should setup your first endpoint, run the post creator and then do the same for your other endpoints.
2 – A ‘list’ endpoint and a ‘detail’ endpoint
This is the most complicated scenario to set up, but is really fairly simple.
In this scenario, your API contains a main endpoint with a list of products or items. It could be 5 products or it could be 5,000. The data for each product is limited and only contains information such as just the name of each product, a description and an ID.
There is also a second endpoint that calls an individual product and this endpoint contains the ‘detailed’ product information such as category, pricing, images and other data. When calling this detail endpoint, we need to pass a product id or an item id and then call this endpoint once for each product within the main list endpoint.
Main Endpoint
Because the API data is a list of multiple items/products, we want to set the Data Type to Multiple Items.
Firstly we need to add our main endpoint and run the Importer and Post Creator for this endpoint. We now have all of the products imported and the posts/products have been created within WordPress.
The Unique Item Key that is set within this endpoint will also be used within the next ‘detail’ endpoint.
Detail Endpoint
We set the second endpoint Data Type to Detail and when this is selected, you will see that a new field shows up called Linked Endpoint. Select your main endpoint from here and these endpoints will become linked. This detail endpoint now uses the Unique Item Key from the main endpoint.
We now need to go back into the endpoint settings to this detail endpoint and set where we want to pass the Unique Item Key. We use (importer:item_key)
to signify where this should be – as shown below.
Each API will be setup differently however, and some APIs will require the item_key to be in the endpoint, the headers or the Body POST Fields
An example of this might be if your full endpoint URL looked like this: https://yourapi.com/v1/product/4567
where 4567 is the product id. In this case you would add the item_key like so: https://yourapi.com/v1/product/(importer:item_key)