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=gb" \
  --data-urlencode "q=amox" \
  --data-urlencode "limit=8"

Response

Response
{
  "country": "gb",
  "suggestions": [
    "Amoxapine",
    "Amoxicillin",
    "AMOXYCILLIN",
    "AMOXICILLIN SODIUM",
    "Amoxil 250mg capsules"
  ]
}

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)
})