Search code examples
phpjsonapietsy

Etsy API target region through active listings instead of user profile


Playing with Etsy's API the documentation doesn't list anything under listings that I've been able to use which would allow someone to code a request like:

$apistateloc = 'florida'; 
$apiurl = 'https://openapi.etsy.com/v2/listings/active?api_key=' . $apikey . '&limit=' . $apilimit . '&region=' . $apistateloc;

The API does mention Associations but even after looking at the associations it would appear after testing the region can be pulled from the userProfile:

$userprofile = json_decode(file_get_contents('https://openapi.etsy.com/v2/users/' . $product->user_id . '/profile?api_key=' . $apikey));
$products[$i]["region"] = $userprofile->results[0]->region;

and the above works. I thought the API Reference for Region would have allowed this:

$theRegion = json_decode(file_get_contents('https://openapi.etsy.com/v2/private/regions?api_key=' . $apikey));
$products[$i]["region_state"] = $theRegion->results[0]->region_name;

but every time I run it I get a result of:

"region_state":"European Union"

is there a way in the Etsy API to target all active listings (https://openapi.etsy.com/v2/listings/active) with a region such as florida? I am trying to make a JSON request that will be based on state, country or city.


Solution

  • Region:

    Represents a collection of countries to which an item ships.

    So with this:

    $theRegion = json_decode(file_get_contents('https://openapi.etsy.com/v2/private/regions?api_key=' . $apikey));
    $products[$i]["region_state"] = $theRegion->results[0]->region_name;
    

    All you are doing is returning a list of possible regions. The return body is actually this:

    {
      "region_id": 11,
      "region_name": "European Union",
      "is_dead": false
    },
     {
      "region_id": 12,
      "region_name": "Europe non-EU",
      "is_dead": false
    },
     {
      "region_id": 13,
      "region_name": "South America",
      "is_dead": true
    },
     {
      "region_id": 14,
      "region_name": "Africa",
      "is_dead": true
    },
     {
      "region_id": 15,
      "region_name": "Central America",
      "is_dead": true
    }
    

    So results[0] is returning the first item, hence always getting European Union.

    What you want to use instead of Region is the location parameter. So your first request string will work perfectly—just replace region with location, like this:

    $apistateloc = 'florida'; 
    $apiurl = 'https://openapi.etsy.com/v2/listings/active?api_key=' . $apikey . '&limit=' . $apilimit . '&location=' . $apistateloc;