21
votes

I'm looking for a way to get the user's location, ideally longitude/latitude but address would work too, from the Alexa Skill Kit request to my custom skill. Also, I don't want to have to have the user link to an account on my app's side.

Is this possible? If so, how?

5

5 Answers

15
votes

Amazon has now (2017-04-05) added this capability. See their blog post about their new Device Address API.

Using this new API you can get the address (either postal code or full address) of the device, as specified in the customer’s device settings.

From that you could use a geocoding API (such as is part of the Google Maps API) to translate the address into location coordinates.

6
votes

As per this thread on the Amazon Developer forums, there is not currently (as of May 2016) a way to get user location via the publicly available APIs. The only skills able to do so, such as Uber or Domino's, are utilizing APIs that are not available through the Alexa Skills Kit. However, there's hope that it may be added, as "Jamie@Amazon" left this reply in that discussion:

Hey there,

Thanks for posting.

This has now been added to the roadmap. Thanks for the feedback.

Jamie

However, at the time of writing, no further update has been provided regarding the implementation of such a feature.

2
votes

As @Tom has pointed out, it is now possible to get the device address in your custom skill. If you are using Python to create your skill, it's pretty easy to implement the new API. I've written a detailed blog post about it here. It also describes how to get the corresponding coordinates for the retrieved address, so it might be useful for you. In short, here is a Python function to get you started. I use the very handy Flask-Ask library for my skill. Thus, it is easy to get the value of the deviceId and consentToken objects. These are included by Alexa in the JSON request sent to your skill. They are needed for constructing the request to the Amazon address API endpoint:

import requests
from flask_ask import Ask, context

def get_alexa_location():
    URL =  "https://api.amazonalexa.com/v1/devices/{}/settings" \
           "/address".format(context.System.device.deviceId)
    TOKEN =  context.System.user.permissions.consentToken
    HEADER = {'Accept': 'application/json',
             'Authorization': 'Bearer {}'.format(TOKEN)}
    r = requests.get(URL, headers=HEADER)
    if r.status_code == 200:
        return(r.json())

The function will return something like this on a successful call:

{u'city': u'Berlin', u'countryCode': u'DE',
u'addressLine1': u'42 Unter den Linden',
u'addressLine2': None, u'stateOrRegion': u'Berlin',
u'districtOrCounty': u'Berlin', u'postalCode': u'10117',
u'addressLine3': None}

You can use geopy to convert this address to coordinates.

2
votes

It is now possible to get the user's real-time geolocation (longitude/latitude) using the Alexa Location Services, which avoids having to integrate with a separate geocoding API as suggested by other answers. See the related blogpost for official information about this feature's release.

Provided that the device is compatible (context.System.device.supportedInterfaces.Geolocation exists), the location services are running and the alexa::devices:all:geolocation:read permission has been granted to your skill , you can retrieve a Geolocation object through the request's context, which will be equivalent to the following JSON payload:

"Geolocation":{ 
    "locationServices": { 
        "access": "ENABLED",
        "status": "RUNNING",   
    },
    "timestamp": "2018-03-25T00:00:00Z+00:00",
    "coordinate": {
        "latitudeInDegrees": 38.2,
        "longitudeInDegrees": 28.3,
        "accuracyInMeters": 12.1 
    },
    "altitude": {
        "altitudeInMeters": 120.1,
        "accuracyInMeters": 30.1
    },
    "heading": { 
        "directionInDegrees": 180.0,
        "accuracyInDegrees": 5.0  
    },
    "speed": { 
        "speedInMetersPerSecond": 10.0,
        "accuracyInMetresPerSecond": 1.1
    }       
}
0
votes

Please follow the below URL to Get location from Alexa Skills Kit (ASK)

URL: https://api.eu.amazonalexa.com/v1/devices/{devicesId}/settings/address/countryAndPostalCode

Your Header would be something like below :

Host:api.eu.amazonalexa.com[keep your host you can get is from developer account during testing from json]
Accept:application/json
Authorization:Bearer [KEEP YOUR apiAccessToken here]

if request went success your will get response as below:

{
    "countryCode": "IN",
    "postalCode": "560102"
}

Make sure you have enabled permission in the Alexa app, and grants the permission of the respective skill please refer the bellow URL for more details of permission configuration

https://developer.amazon.com/blogs/alexa/post/0c975fc7-17dd-4f5c-8343-a37024b66c99/alexa-skill-recipe-using-the-device-address-api-to-request-information