AWS

Create Your Own Search Serverless Application

By Michael Palermo | 30 January 2020

Try HERE Maps

Create a free API key to build location-aware apps and services.

Get Started

Whenever we need to know what is here or there, the Search (Places) API provides many dimensions of querying points of interest around the world. AWS developers can graft in this search feature quickly within their own development environment using the AWS Serverless Application Repository

This post will show how you can deploy HERE Search (Places) Serverless Application for use in your development projects. This will allow you to query location data from HERE via your own custom API! To accomplish this, you will need credentials at the following:


Deploying the HERE Search Serverless App

After account credentials are confirmed, to find any serverless application, browse to the AWS Serverless Application Repository.

AWS Serverless Application Browse

After clicking the "Browse all applications" button, you will be prompted to do your search. Type "HERE Places", then select the Places result as shown below.

2020sar-places-find

You will be redirected to the landing page for the Places application. Under "Application settings" enter your HERE REST API Key (obtained from your developer profile) in the highlighted area shown below, then deploy.

2020sar-places-credentials

It will take a little time for the deployment process to complete. You are prepared for the next step once you see the "Your application has been deployed" message shown.


Accessing Your Search Application

Part of the newly created Search application is the API endpoint to execute it. The following screen capture shows how to execute a simple test (food places in Phoenix, AZ USA) via the AWS API Gateway:

2020sar-places-api-test

 

Accessing from HTML Client

For testing from HTML client, you may need to go into the source code of the lambda function and uncomment the headers as shown below:

2020sar-places-cors

Now consider a request made to the Search Application from an HTML client:


const display = document.getElementById("display");
const url = "https://your-api-id.execute-api.us-east-1.amazonaws.com/";
const stage = "Prod";
const resource = "places/v1/autosuggest"
const atin = "at";
const geo = "33.4345,-112.0674";

// call search serverless application 
function invokeGeoAPI(search) {
    fetch(`${url}${stage}/places/api/${resource}?${atin}=${geo}&q=${search}`)
        .then(response => response.json())
        .then(response => {
            console.log(response);
            display.innerHTML = "";
            let items = response.results.items || response.results;
            displayItems(items); // call helper function
        });
}

// when user clicks search button
function processForm() {
    let search = document.getElementById("searchInput").value;
    console.log(search);
    if (search == "") {
        alert("Need a search!");
        return false;
    }
    invokeGeoAPI(search); // function defined above
}

The above script calls a constructed URL from all the consts declared which points to the new search application via Amazon API Gateway. You must replace the value of your-api-id with the value generated from your API Gateway when the Search app was deployed. After the fetch completes the entire response is processed in a helper function which displays to screen.

For more information, please enjoy our recorded session on this topic: