NewConnect Claude, Gemini, ChatGPT, and other AI agents to API Ninjas via our MCP server

How to Calculate Sales Tax

Sales Tax APIUpdated August 28, 2026
Create a free API key and it will be pre-filled into every code sample on this page. No credit card required.
To calculate sales tax, multiply the price by the combined tax rate: sales tax = price × rate. A $100 purchase in Beverly Hills (ZIP 90210), where the combined rate is 9.75%, owes $9.75, for a $109.75 total. The arithmetic is one line — the real problem is knowing the right rate, because it differs across 13,000+ US jurisdictions. This guide covers both: the formula, and code that looks up the exact rate for any ZIP code.

Prerequisites

For the lookup steps you need three things (the formula needs nothing at all):

  • Python 3.8 or newer.
  • The requests library.
  • A free API Ninjas API key — issued instantly, no credit card. If you already have an account, it's on your profile page.
shell
pip install requests

Step 1: Look up the combined rate

The rate you charge is almost never just the state rate — counties, cities, and special districts each add their own. The Sales Tax API returns the full breakdown for any ZIP code (or city and state):

rate_lookup.py
import requests response = requests.get( 'https://api.api-ninjas.com/v1/salestax', params={'zip_code': '90210'}, headers={'X-Api-Key': 'YOUR_API_KEY'}, timeout=10, ) response.raise_for_status() rate_info = response.json()[0] print(f"Combined rate: {float(rate_info['total_rate']) * 100:.2f}%") # Combined rate: 9.75%
api.api-ninjas.com
GET/v1/salestax?zip_code=90210
200 OKapplication/json
[ { "zip_code": "90210", "state_rate": "0.06", "city_rate": "0.01", "county_rate": "0.0025", "additional_rate": "0.025", "total_rate": "0.0975" } ]

Rates are decimals — "0.0975" means 9.75% — and they sum: for 90210, California's 6% + LA County's 0.25% + Beverly Hills' 1% + 2.5% of special districts = 9.75%. Combined rates nationwide run from 0% (Delaware, Montana, New Hampshire, Oregon) past 10% in the highest-tax parts of Louisiana and Alabama. Detailed breakdown fields beyond the state rate are available on premium plans.

Step 2: Apply the formula

With the rate in hand, the calculation is elementary:

What you wantFormulaExample (9.75%)
Sales tax owedprice × rate$100 × 0.0975 = $9.75
Total with taxprice × (1 + rate)$100 × 1.0975 = $109.75
Pre-tax price from a totaltotal ÷ (1 + rate)$109.75 ÷ 1.0975 = $100.00
formula.py
price = 100.00 rate = 0.0975 # 9.75% as a decimal sales_tax = price * rate # 9.75 total = price * (1 + rate) # 109.75 print(f'Tax: ${sales_tax:.2f}') print(f'Total: ${total:.2f}')

The one mistake to avoid: convert the percentage to a decimal before multiplying — 9.75% is 0.0975, not 9.75.

Step 3: Calculate automatically

If you want dollar amounts rather than rates — what a receipt actually shows — the Sales Tax Calculator API does the multiplication for you, itemized per jurisdiction:

calculator.py
response = requests.get( 'https://api.api-ninjas.com/v1/salestaxcalculator', params={'zip_code': '90210', 'amount': 100}, headers={'X-Api-Key': 'YOUR_API_KEY'}, timeout=10, ) response.raise_for_status() breakdown = response.json()[0] print(f"Total tax: ${breakdown['total_tax']}") # Total tax: $9.75
api.api-ninjas.com
GET/v1/salestaxcalculator?zip_code=90210&amount=100
200 OKapplication/json
[ { "zip_code": "90210", "pre_tax_amount": "100", "state_rate": 0.06, "city_rate": 0.01, "county_rate": 0.0025, "additional_rate": 0.025, "total_rate": 0.0975, "state_tax": 6, "city_tax": 1, "county_tax": 0.25, "additional_tax": 2.5, "total_tax": 9.75 } ]

state_tax, county_tax, city_tax, and additional_tax arrive separately, with total_tax summing them — no float arithmetic on your side.

Step 4: Calculate backwards

Given a receipt total and the rate, recover the pre-tax price by dividing by 1 + rate. Multiplying the total by the rate is the classic mistake — it overstates the tax:

reverse.py
total_paid = 109.75 rate = 0.0975 pre_tax_price = total_paid / (1 + rate) # 100.00 — divide, don't multiply sales_tax = total_paid - pre_tax_price # 9.75 print(f'Pre-tax price: ${pre_tax_price:.2f}') print(f'Sales tax: ${sales_tax:.2f}')

Step 5: Wire it into checkout

In production, look the tax up per order at request time — rates change during the year, and hard-coded tables drift. The client shape is the usual one: key from the environment, one shared session, a timeout:

checkout.py
import os import requests session = requests.Session() session.headers['X-Api-Key'] = os.environ.get('API_NINJAS_KEY', 'YOUR_API_KEY') def sales_tax_for_order(zip_code: str, subtotal: float) -> dict: """Return the full tax breakdown for an order at checkout time.""" response = session.get( 'https://api.api-ninjas.com/v1/salestaxcalculator', params={'zip_code': zip_code, 'amount': subtotal}, timeout=10, ) response.raise_for_status() return response.json()[0] order = sales_tax_for_order('90210', 249.99) print(f"Subtotal: ${order['pre_tax_amount']}") print(f"Tax rate: {float(order['total_rate']) * 100:.2f}%") print(f"Tax due: ${order['total_tax']:.2f}")

For the few ZIP codes that span multiple tax districts, pass street_address together with city and state for an exact jurisdiction match. And if you sell across states, whether you must collect at all is an economic-nexus question — the /v1/salestaxnexus endpoint checks your sales figures against every state's thresholds.

Where to go from here

For European transactions the VAT Rates API covers every EU member state, and the Income Tax and Property Tax APIs round out the US tax picture. The complete script is below.

sales_tax.py

The complete script from this guide: rate lookup plus full dollar breakdown, ZIP and amount from the command line.

Frequently asked questions

What is the formula for calculating sales tax?

Sales tax = purchase price × sales tax rate. For the total, multiply the price by (1 + rate). A $100 purchase at a 9.75% combined rate owes $9.75 in tax and costs $109.75 at checkout.

How do I calculate sales tax backwards from a total?

Divide the total by (1 + rate) to recover the pre-tax price, then subtract. If you paid $109.75 at 9.75%, the pre-tax price is $109.75 / 1.0975 = $100.00 and the tax portion is $9.75.

Which US states have no sales tax?

Delaware, Montana, New Hampshire, and Oregon have none at all. Alaska has no statewide sales tax, but many Alaskan municipalities charge a local one.

Why is my rate higher than my state’s rate?

Counties, cities, and special districts stack their own percentages on top of the state rate. What you actually pay is the combined total — the total_rate field in the API response.

How accurate are lookups by ZIP code?

ZIP codes usually map cleanly to one combined rate, but a few span multiple tax districts. For those, the Sales Tax API accepts a street_address parameter alongside city and state for an exact match.

Is sales tax the same as VAT?

No. Sales tax is charged once, at the final sale, and is a US system. VAT is collected at each supply-chain stage and used across the EU and much of the world — see the VAT Rates API for European rates.