如果您正在建立需要準確計算銷售稅的應用程序,那麼 zip.tax API 是一個出色的整合工具。本指南將引導您了解如何在 Python 應用程式中設定和使用 zip.tax API。
開始之前,請確保您具備以下條件:
為了發出 HTTP 請求,我們將使用 Python 的標準 requests 套件。此外,我們將使用 json 來解析 JSON 回應。
建立新的專案目錄並初始化新模組:
mkdir ziptax-python && cd ziptax-python
這是一個簡單的 Python 應用程式的完整範例,該應用程式查詢 zip.tax API 以獲取銷售稅資訊。
import requests import json class Response: def __init__(self, data): self.version = data.get("version") self.r_code = data.get("rCode") self.results = [Result(result) for result in data.get("results", [])] self.address_detail = AddressDetail(data.get("addressDetail", {})) class Result: def __init__(self, data): self.geo_postal_code = data.get("geoPostalCode") self.geo_city = data.get("geoCity") self.geo_county = data.get("geoCounty") self.geo_state = data.get("geoState") self.tax_sales = data.get("taxSales") self.tax_use = data.get("taxUse") self.txb_service = data.get("txbService") self.txb_freight = data.get("txbFreight") self.state_sales_tax = data.get("stateSalesTax") self.state_use_tax = data.get("stateUseTax") self.city_sales_tax = data.get("citySalesTax") self.city_use_tax = data.get("cityUseTax") self.city_tax_code = data.get("cityTaxCode") self.county_sales_tax = data.get("countySalesTax") self.county_use_tax = data.get("countyUseTax") self.county_tax_code = data.get("countyTaxCode") self.district_sales_tax = data.get("districtSalesTax") self.district_use_tax = data.get("districtUseTax") self.district1_code = data.get("district1Code") self.district1_sales_tax = data.get("district1SalesTax") self.district1_use_tax = data.get("district1UseTax") self.district2_code = data.get("district2Code") self.district2_sales_tax = data.get("district2SalesTax") self.district2_use_tax = data.get("district2UseTax") self.district3_code = data.get("district3Code") self.district3_sales_tax = data.get("district3SalesTax") self.district3_use_tax = data.get("district3UseTax") self.district4_code = data.get("district4Code") self.district4_sales_tax = data.get("district4SalesTax") self.district4_use_tax = data.get("district4UseTax") self.district5_code = data.get("district5Code") self.district5_sales_tax = data.get("district5SalesTax") self.district5_use_tax = data.get("district5UseTax") self.origin_destination = data.get("originDestination") class AddressDetail: def __init__(self, data): self.normalized_address = data.get("normalizedAddress") self.incorporated = data.get("incorporated") self.geo_lat = data.get("geoLat") self.geo_lng = data.get("geoLng") def get_sales_tax(address, api_key): try: api_url = f"https://api.zip-tax.com/request/v50?key={api_key}&address={requests.utils.quote(address)}" response = requests.get(api_url) if response.status_code != 200: raise Exception(f"Unexpected status code: {response.status_code}") response_data = response.json() return Response(response_data) except Exception as e: print(f"Error fetching sales tax: {e}") return None def main(): api_key = "your_api_key_here" # Replace with your actual API key address = "200 Spectrum Center Dr, Irvine, CA 92618" # Example Address tax_info = get_sales_tax(address, api_key) if tax_info: print(f"Normalized Address: {tax_info.address_detail.normalized_address}") print(f"Address Lat/Lng: {tax_info.address_detail.geo_lat}, {tax_info.address_detail.geo_lng}") if tax_info.results: print(f"Rate: {tax_info.results[0].tax_sales * 100:.2f}%") if __name__ == "__main__": main()
將程式碼儲存到檔案(例如main.py),然後執行程式:
python main.py
您應該會看到與此類似的輸出:
Normalized Address: 200 Spectrum Center Dr, Irvine, CA 92618-5003, United States Address Lat/Lng: 33.652530, -117.747940 Rate: 7.75%
將 zip.tax API 整合到您的 Python 應用程式中非常簡單。透過遵循本指南,您可以使用基於地址的準確銷售稅資訊來增強您的申請。更多詳情請參考官方文件。
如果您有任何問題或回饋,請隨時在下面發表評論。快樂編碼!
以上是將 zip.zax 銷售稅 API 整合到您的 Python 應用程式中的詳細內容。更多資訊請關注PHP中文網其他相關文章!