Skip to main content
All CollectionsPaaS API
Autocomplete Keyword API Example
Autocomplete Keyword API Example

This is a complete example using MetaLocator's keyword autocomplete endpoint

Michael Fatica avatar
Written by Michael Fatica
Updated over a month ago

The below is a complete example of the MetaLocator PaaS API's keyword_suggest method.

The expected output is shown below:

Be sure to update your API Key and Interface (Itemid)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Autocomplete Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
$(document).ready(function () {
$("#keyword").autocomplete({
source: function (request, response) {
// Make an API request to fetch keyword suggestions
$.ajax({
url: 'https://different.domain/webapi/api/keyword_suggest',
dataType: 'jsonp',
data: {
apikey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
Itemid: 'NNNNN',
ml___compact: 0,
jsonp: 1,
keyword: request.term // Get the input value
},
success: function (data) {

// Transform the API response into a format jQuery UI can use
const suggestions = data.suggestions.map(function (item) {
return {
label: item.id + ' ' + item.text, // Display the 'text' property in the autocomplete dropdown
value: item.text, // Set the input field's value to 'text' when an item is selected
id: item.id, // Store the 'id' for later use
name: item.name, // Optionally store the 'name'
tagidlist: item.tagidlist // Optionally store the 'tagidlist'
};
});
// Pass the formatted suggestions to the response callback
response(suggestions);
},
error: function () {
// Handle any errors here
response([]);
}
});
},
minLength: 2, // Minimum number of characters before autocomplete starts
select: function (event, ui) {
// Do something when an item is selected
console.log("Selected: " + ui.item.value);
}
});
});
</script>
</head>
<body>
<h1>Keyword Autocomplete</h1>
<label for="keyword">Start typing:</label>
<input id="keyword" type="text" placeholder="Type something...">
</body>
</html>

Did this answer your question?