Blog

Building a medication typeahead that members can use

Most medication fields fail the same way. The member types “tylenol” and gets forty rows of package codes, strengths they have never heard of and manufacturer names. They pick one at random, or they give up and type into the free-text box, and now your data is a guess.

A good medication field asks two short questions instead of one long one.

Step 1: what is it called?

Search should return the names people say: the brand on the box, the ingredient on the label, and the everyday name (“vitamin D3”). It should leave packages out and return one hit per thing a person would recognize:

GET /v1/drugs/search?q=tylenol&limit=8

Trimmed to three hits:

{
  "hits": [
    { "id": "pw_g_99ce39cdd1006724", "display_name": "Tylenol", "subtitle": "acetaminophen", "pick_level": "brand" },
    { "id": "pw_g_d86136eebce76566", "display_name": "Tylenol PM", "subtitle": "acetaminophen, diphenhydramine", "pick_level": "brand" },
    { "id": "pw_g_47dc27c1068eb4dd", "display_name": "Tylenol with Codeine", "subtitle": "acetaminophen, codeine", "pick_level": "brand" }
  ]
}

Order matters more than recall here. The medicine named exactly comes first, then its common forms, then brands and combinations. People rarely scroll a suggestion list, so the first few rows have to be right. The subtitle is there for the cases a name alone can’t settle: further down the same list, “Tylenol Precise Pain Relieving” is lidocaine, not acetaminophen. The medication autocomplete API page has every parameter and response field.

Step 2: which one?

Once the member has picked a name, ask for form and strength from the choices that exist for it:

GET /v1/drugs/pw_g_99ce39cdd1006724/form-options

For Tylenol, trimmed:

{
  "form_options": {
    "forms": [
      { "label": "Tablet", "value": "tablet" },
      { "label": "Liquid suspension", "value": "oral_suspension" },
      { "label": "Chewable tablet", "value": "chewable_tablet" }
    ],
    "strengths": [
      { "label": "325 mg", "value": "325 MG", "forms": ["tablet", "capsule"] },
      { "label": "500 mg", "value": "500 MG", "forms": ["tablet", "oral_powder"] },
      { "label": "160 mg per 5 mL", "value": "32 MG/ML", "forms": ["oral_suspension"] }
    ]
  }
}

You get the forms (tablet, liquid, patch) and the strengths each form comes in, with labels written for people (“160 mg per 5 mL”) and values to send back. Each strength lists the forms it exists in, so once the member picks a form, show only the strengths that match. Two dropdowns, both short, and every combination they can make is a real product.

That is the whole field. Packages and NDCs are for pharmacy and billing; a member adding what they take never needs to see one.

The client details that matter

Wait for three characters before you search. Below that only whole words match, so one or two letters rarely find anything, and every keystroke would be a request.

Then debounce and cancel. Wait about 250 ms after the last keystroke, and abort the request before it when a new one starts. Otherwise a slow answer for “tyl” can land after the answer for “tylenol” and replace it. Each search request is also a lookup on the meter, so a keystroke you don’t send is one you don’t pay for.

let pending;
let timer;

input.addEventListener("input", () => {
  clearTimeout(timer);
  const q = input.value.trim();
  if (q.length < 3) return render([]);
  timer = setTimeout(async () => {
    pending?.abort();
    pending = new AbortController();
    try {
      // Your server calls Pillwise with the key; the browser never sees it.
      const res = await fetch(`/api/med-search?q=${encodeURIComponent(q)}`, { signal: pending.signal });
      render((await res.json()).hits);
    } catch (e) {
      if (e.name !== "AbortError") showError();
    }
  }, 250);
});

Keep the key on your server and proxy the search through your backend. An API key in browser code is a key anyone can use. The proxy can be this small:

// GET /api/med-search?q=… on your server.
export async function medSearch(q) {
  const url = `https://api.pillwise.dev/v1/drugs/search?q=${encodeURIComponent(q)}&limit=8`;
  const res = await fetch(url, { headers: { Authorization: `Bearer ${process.env.PILLWISE_KEY}` } });
  if (!res.ok) throw new Error(`search failed: ${res.status}`);
  return res.json();
}

Make it a real combobox: role="combobox" on the input, role="listbox" on the menu, aria-activedescendant for the highlighted row, and arrow keys, Enter and Escape that do what people expect. Screen-reader users add medications too.

Let members finish without a match, too. Some will type something your catalog doesn’t know. Keep what they typed, flag it for review and let them move on; a blocked form is worse than an unmatched row.

Store the pick, not the text

Save the id you got back, the form and strength values, and the date of the answer:

{
  "drug_id": "pw_g_99ce39cdd1006724",
  "form": "tablet",
  "strength": "500 MG",
  "typed": "tylenol",
  "as_of": "2026-10-22"
}

Then the entry still means the same thing when the catalog changes next month, and you can show “as of” when someone asks where it came from. If a clinical system downstream wants codes, resolve them from the pick, not from the text; NDC to RxCUI, and back covers which code to keep.

You can try this flow in the browser: each step shows the real call it makes. When you want it in your app, get a sandbox key; the sandbox gives you 500 lookups a month and doesn’t ask for a card.

Ship the medication field this sprint. Keep a drug catalog forever.

Sandbox is 500 lookups a month with no card. When you go live, Launch is $99 a month for one production app.