APITube Help Center

How to Highlight Matching Terms in Search Results

Return short excerpts that mark matching search terms

Kent Hudson

Written by Kent Hudson

June 26, 2026

Updated October 16, 2026

Open this example in the API Playground ↗

How to highlight matching terms in search results

Add hl=true to a keyword search to get excerpts with matching terms marked. By default, matches appear inside <em> tags in a separate highlighting object, keyed by article ID.

curl "https://api.apitube.io/v1/news/everything?title=bitcoin&hl=true&api_key=YOUR_API_KEY"

You can also send your key in the X-API-Key header. Highlighting needs search terms; enabling it on a request without terms does not create excerpts.

How does highlighting work in the News API?

Set hl=true or hl=1 alongside a title search. The API returns matching excerpts from the fields you choose. Each field contains an array of strings:

{
  "status": "ok",
  "results": [...],
  "highlighting": {
    "12345": {
      "title": [ "Breaking: <em>Bitcoin</em> reaches new all-time high" ],
      "description": [ "...The price of <em>Bitcoin</em> surged past $100,000 today..." ]
    }
  }
}

Dots around an excerpt show that it comes from a longer text. If there are no excerpts, the response omits highlighting.

Which fields can you highlight?

hl.fl accepts title, description and body, separated by commas. The default is title,description.

curl "https://api.apitube.io/v1/news/everything?title=artificial intelligence&hl=true&hl.fl=title,description,body&api_key=YOUR_API_KEY"

Add body when you need context from the article text. Unsupported fields return HTTP 400 with ER0322.

How to control snippet size and count

Use two parameters:

  • hl.fragsize: excerpt length in characters. Default 150; supported range 50–500.
  • hl.snippets: maximum excerpts per field. Default 3; supported range 1–10.

Values outside these ranges are adjusted to the nearest limit.

curl "https://api.apitube.io/v1/news/everything?title=climate change&hl=true&hl.fragsize=300&hl.snippets=5&api_key=YOUR_API_KEY"

Increase the length for more surrounding text, or the count for more separate matches in a field.

How to change the highlight tags

Set hl.tag.pre and hl.tag.post to change the opening and closing markers. The defaults are <em> and </em>.

curl "https://api.apitube.io/v1/news/everything?title=bitcoin&hl=true&hl.tag.pre=<mark>&hl.tag.post=</mark>&api_key=YOUR_API_KEY"

Set both parameters for a matching pair. When building a URL, let your HTTP client encode tag characters such as < and >.

Does highlighting match word variations?

It can mark related words and word forms as well as the exact text you entered. Check the returned excerpt rather than assuming every marked word is identical to your search term. Available variations depend on the term and language.

Common Questions

Which endpoints support highlighting?

Use highlighting on /v1/news/everything, /v1/news/top-headlines, /v1/news/story, /v1/news/category, /v1/news/topic, /v1/news/industry and /v1/news/entity. You can also select article fields to reduce the response size.

Why is the highlighting object empty or missing?

Check that the request includes search terms and that those terms occur in the fields selected by hl.fl. Terms shorter than two characters are skipped. No matching excerpts means no highlighting object.

What errors can highlighting return?

HTTP 400 errors include ER0320 for a list with no usable fields, ER0321 for more than five field entries and ER0322 for unsupported names. Use title, description or body. See the News API parameters reference.

Does highlighting cost an extra request?

No. Excerpts arrive in the same search response. You can combine highlighting with facets in one call.


Related Articles