Docs / Guides

Typeahead suggestions

GET /v1/drugs/suggestions returns distinct names while a user types, for autocomplete fields.

Request

GET /v1/drugs/suggestions
curl -G https://api.medifontis.com/v1/drugs/suggestions \
  -H "Authorization: Bearer $MEDIFONTIS_API_KEY" \
  --data-urlencode "country=es" \
  --data-urlencode "q=enan" \
  --data-urlencode "limit=8"

Response

Response
{
  "country": "es",
  "suggestions": [
    "ENANDOL 12,5 mg/g GEL",
    "ENANGEL 12,5 mg/g GEL",
    "ENANTYUM 12,5 mg COMPRIMIDOS",
    "ENANTYUM 25 MG CAPSULAS DURAS",
    "ENANTYUM 25 MG GRANULADO EN SOBRE"
  ]
}

Ranking

Exact names sort first, then names that start with the query, then other matches, shorter names first. Suggestions come from every source, including ingredient and classification names.

Build a responsive autocomplete

  • Wait until the user has typed at least 3 characters.
  • Debounce keystrokes by 150 to 250 milliseconds.
  • Ignore responses for a query the user has already changed.
  • Call your own backend, which adds the key, instead of Medifontis directly.
Browser
let latest = 0
let timer
input.addEventListener('input', () => {
  clearTimeout(timer)
  const q = input.value.trim()
  if (q.length < 3) return
  timer = setTimeout(async () => {
    const request = ++latest
    const response = await fetch(`/api/medicine-suggestions?q=${encodeURIComponent(q)}`)
    const { suggestions } = await response.json()
    if (request === latest) render(suggestions)
  }, 200)
})