Build an Autocomplete Search Box
Build a search box with the Suggest endpoints. Send the user's text as prefix, display the matches, and open the selected subject's news feed.
Step 1: Suggest entities by prefix
Send the search text as prefix. Each match includes a type such as person, organization, location or brand, and a links.self URL for its articles.
curl "https://api.apitube.io/v1/suggest/entities?prefix=tesl&api_key=YOUR_API_KEY"
import requests
response = requests.get(
"https://api.apitube.io/v1/suggest/entities",
params={
"prefix": "tesl",
"api_key": "YOUR_API_KEY",
},
)
print(response.json())
const params = new URLSearchParams({ "prefix": "tesl", "api_key": "YOUR_API_KEY" });
const response = await fetch(`https://api.apitube.io/v1/suggest/entities?${params}`);
const data = await response.json();
console.log(data);
$query = http_build_query(["prefix" => "tesl", "api_key" => "YOUR_API_KEY"]);
$response = file_get_contents("https://api.apitube.io/v1/suggest/entities?$query");
$data = json_decode($response, true);
print_r($data);
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
u, _ := url.Parse("https://api.apitube.io/v1/suggest/entities")
q := u.Query()
q.Set("prefix", "tesl")
q.Set("api_key", "YOUR_API_KEY")
u.RawQuery = q.Encode()
resp, _ := http.Get(u.String())
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var data []map[string]any
json.Unmarshal(body, &data)
fmt.Println(data)
}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Example {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.apitube.io/v1/suggest/entities?prefix=tesl&api_key=YOUR_API_KEY"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
Write a script in your preferred language that calls the APITube News API:
GET https://api.apitube.io/v1/suggest/entities?prefix=tesl
Read the API key from an environment variable (do not hardcode it), handle request
errors, and print the key fields of each result.
Docs: https://docs.apitube.io/platform/news-api/everything
Display name and type from the match, and keep links.self for the next step:
[
{
"id": 474,
"name": "Tesla Robotaxi",
"type": "brand",
"links": {
"self": "https://api.apitube.io/v1/news/entity/474",
"wikipedia": "https://en.wikipedia.org/wiki/Tesla_Robotaxi",
"wikidata": "https://www.wikidata.org/wiki/Q135277686"
},
"metadata": {
"name": "Tesla Robotaxi",
"type": "brand",
"category": "general",
"brand_owner": { "id": "org-481", "name": "Tesla, Inc." },
"description": "Ride-hailing service operated by Tesla, Inc"
}
}
]Wait about 150 ms after the user stops typing before requesting suggestions. This avoids a request for every keystroke.
Step 2: Open articles about the selected entity
When the user selects a match, open its links.self URL to get the entity feed. Add filters such as published_at, language.code and sort.by, or request another page.
curl "https://api.apitube.io/v1/news/entity/474?sort.by=published_at&api_key=YOUR_API_KEY"
import requests
response = requests.get(
"https://api.apitube.io/v1/news/entity/474",
params={
"sort.by": "published_at",
"api_key": "YOUR_API_KEY",
},
)
print(response.json())
const params = new URLSearchParams({ "sort.by": "published_at", "api_key": "YOUR_API_KEY" });
const response = await fetch(`https://api.apitube.io/v1/news/entity/474?${params}`);
const data = await response.json();
console.log(data);
$query = http_build_query(["sort.by" => "published_at", "api_key" => "YOUR_API_KEY"]);
$response = file_get_contents("https://api.apitube.io/v1/news/entity/474?$query");
$data = json_decode($response, true);
print_r($data);
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
u, _ := url.Parse("https://api.apitube.io/v1/news/entity/474")
q := u.Query()
q.Set("sort.by", "published_at")
q.Set("api_key", "YOUR_API_KEY")
u.RawQuery = q.Encode()
resp, _ := http.Get(u.String())
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var data map[string]any
json.Unmarshal(body, &data)
fmt.Println(data)
}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Example {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.apitube.io/v1/news/entity/474?sort.by=published_at&api_key=YOUR_API_KEY"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
Write a script in your preferred language that calls the APITube News API:
GET https://api.apitube.io/v1/news/entity/474?sort.by=published_at
Read the API key from an environment variable (do not hardcode it), handle request
errors, and print the key fields of each result.
Docs: https://docs.apitube.io/platform/news-api/everything
Suggest categories, topics and industries
Use the same approach to suggest categories, topics and industries:
/v1/suggest/categories— category picker./v1/suggest/topics— topic picker./v1/suggest/industries— industry picker.
Each endpoint returns matches with an id. Use it in the matching /v1/news/everything filter: category.id, topic.id or industry.id.
Tip: the
prefixparameter requires at least one character — skip the call while the input is empty.