DEV Community

Carra Globe
Carra Globe

Posted on

A 700-product HS code search with zero API calls

Most product search tutorials start by reaching for a hosted index. We went the other way. The free HS Code Finder on our site searches more than 700 products and returns a likely 6-digit Harmonized System code, and it makes no network request after the page loads. Everything lives in one inline script.

This post covers why we did it that way, what the search has to get right, and the trade-offs we accepted.

Why an HS code search is harder than it looks

An HS code is the six-digit customs classification every shipment carries. Get it wrong and the goods can be held, re-rated for duty, or flagged for an audit of earlier entries. The people searching are not customs specialists. They type "tv", "kiosk" or "solar pannel" and expect something sensible back.

That gives the search three jobs. It has to tolerate typos, it must not drown short words in false matches, and it has to admit when it is guessing. A search box that bluffs is worse than one that says nothing, because a confident wrong code can end up on a commercial invoice.

Ship the data, not a server

The product list is a JSON block parsed once with JSON.parse when the script starts. Each row carries a product name, aliases, a description, the code, its chapter and a short note on why that heading applies. Lower-cased copies of the searchable fields are prepared once at load, so a keystroke compares strings rather than re-normalising the whole dataset every time.

The full script, data included, comes to roughly 225 KB before compression. For a tool that answers in milliseconds offline once loaded, we judged that a fair price. There is no rate limit, no API key to leak and nothing to break when a third-party service changes its terms.

Ranking rules that matter more than the algorithm

The scoring is plain arithmetic, and the ordering of the tiers carries most of the weight.

  1. A product's own name beats a mention in another product's alias list. Searching "core switch" should land on the switch row, not on a rack accessory that happens to mention switches.
  2. A full-phrase alias match beats scattered word hits. "Lithium battery" as a phrase is a stronger signal than "lithium" in one row and "battery" in another.
  3. Short single-word queries use whole-word matching. Without it, "tv" matches every row containing the letters, including "cctv". With it, "tv" returns colour televisions under 8528.72 first.
  4. Typo tolerance only earns partial credit. A Levenshtein distance check lets "solar pannel" find the solar panel row, but a near-miss can never outrank an exact hit elsewhere.

That last rule is the one we got wrong first. When fuzzy matches scored too generously, a misspelled query could beat a correctly spelled synonym on another row. Capping the rescue tier below the partial-match floor fixed it.

Admit weak matches

When the best score falls below a threshold, the tool shows a weak-match notice and suggests the closest term instead of presenting the top row as an answer. In an early version, "kiosk" landed on an unrelated food heading because a stray alias scored higher than anything sensible. Today it returns self-service kiosks under 8471.41, and the threshold is there for the queries we have not thought of yet.

This is the part of the build I would copy into any domain search where the answer has consequences. Ranking tells you the order. A threshold tells you whether the first item deserves to be called an answer at all.

Small things that make it feel fast

  • Input is debounced at 250 ms, so a fast typist triggers one search rather than eight.
  • State goes into the URL with history.replaceState, never pushState, so a search can be shared without filling the back button with every keystroke.
  • Recent searches sit in localStorage, so a returning visitor can pick up where they left off.
  • The whole thing runs inside an IIFE in strict mode with its CSS scoped to one wrapper, because it is pasted into a WordPress page builder and must not collide with the theme.

What it deliberately does not do

It does not claim to classify goods. HS codes are harmonised only to six digits, and every country adds its own national digits on top. The tool shows how many digits each of 16 destinations adds and links to the official UK, EU and US tariff look-ups, so the final code is checked where it is published.

Customs authorities decide classification on the actual goods, their function and how they are presented. A search box can get you to the right heading. It cannot sign the declaration.

Where the hard cases go

The edge cases in our data are almost all IT hardware. Servers, GPU boards, network switches and the batteries inside them each sit in different chapters depending on what exactly is being shipped. When classification and the importer role overlap, that is the work our importer of record service carries, because the importer named on the entry is the party answerable for the code.

If you are building something similar for another regulated domain, I would start with the threshold before the fuzzy matching. Getting a sensible answer for "tv" is nice. Refusing to invent an answer for "kiosk" is what keeps the tool honest.

For the headings that trip up IT shipments most often, read our field guide to HS codes for electronics and tech equipment.

Top comments (1)

Collapse
 
ibuildinpublic profile image
Ehsan •

Typo tolerance with no network call is neat, and I like that you mention it should admit when it's unsure. Did you end up using a simple edit distance or something lighter for stuff like "solar pannel"?