Magento REST API - How to determine the API base URL?
Asked Answered
F

1

5

I'm running Magento 2.2.5 and was having trouble working out what the URL was for making API requests. For example to GET a list of countries I had seen the following syntax used:

/rest/default/V1/directory/countries

which has worked on some stores, but I was getting this error:

{
"message" : "Specified request cannot be processed.",
"trace" : null
}

I started playing around with the URL format and removed the "default" and used this instead:

/rest/V1/directory/countries

and the requests were then successful. How does one determine what the base URL to use for Magento REST API requests? I haven't been able to find this documented so far.

Fenton answered 9/6, 2019 at 10:38 Comment(1)
THIS!!! THIS is exactly why I hate Magento....Mezuzah
K
11

The syntax of Magento 2 Api is

http://<:host:>/rest/<:store_code:>/<:api_path:>

Here store_code can be any one of the "store view" of your Magento2 instance. You can get the store code in admin panel. You can use store_code to get information specific to that Store View via API. By default Magento2 installation comes with 1 website, 1 store & 1 store view (this store-view has code "default", in your case this might have changed hence you get the error).

The architecture can be understand with the help of image Reference Image Ref: https://docs.magento.com/m2/ce/user_guide/stores/websites-stores-views.html

  • When you specify store_code it checks information for that particular Store-View. If the specified store_code is not found in the database, Api returns "Specified request cannot be processed." message.

  • When you do not specify store_code it first finds default Website, then finds default Store for this Website & then finds default Store View Associated to this Store. And finally it returns information for this default Store-View. The association of default store & store-view can be changed from admin panel. Reference Image

Hence the URL for API should be below in case you need store specific information. store_code can be set to all if you need information for all the stores.

http://<:host:>/rest/<:store_code:>/<:api_path:>

Kex answered 9/6, 2019 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.