-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
69 lines (56 loc) · 2.1 KB
/
basic-usage.ts
File metadata and controls
69 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Basic usage example for ZipTax SDK
*
* Usage:
* ZIPTAX_API_KEY=your-api-key npm run example:basic
*/
import { ZiptaxClient } from '../src';
async function main() {
// Initialize the client with your API key from environment variable
const apiKey = process.env.ZIPTAX_API_KEY || 'your-api-key-here';
if (apiKey === 'your-api-key-here') {
console.error('Error: Please set ZIPTAX_API_KEY environment variable');
console.error('Usage: ZIPTAX_API_KEY=your-api-key npm run example:basic');
process.exit(1);
}
const client = new ZiptaxClient({
apiKey,
});
try {
// Get sales tax by address
const taxByAddress = await client.getSalesTaxByAddress({
address: '200 Spectrum Center Drive, Irvine, CA 92618',
});
console.log('Tax Rate by Address:');
console.log('Total Rate:', taxByAddress.taxSummaries?.[0]?.rate);
console.log('Base Rates:', taxByAddress.baseRates);
console.log('---');
// Get sales tax by geolocation
const taxByGeo = await client.getSalesTaxByGeoLocation({
lat: '33.65253',
lng: '-117.74794',
});
console.log('Tax Rate by Geolocation:');
console.log('Total Rate:', taxByGeo.taxSummaries?.[0]?.rate);
console.log('Address:', taxByGeo.addressDetail.normalizedAddress);
console.log('---');
// Get rates by postal code
const ratesByPostalCode = await client.getRatesByPostalCode({
postalcode: '92694',
});
console.log('Rates by Postal Code:');
console.log('Postal Code:', ratesByPostalCode.results[0]?.geoPostalCode);
console.log('Total Sales Tax:', ratesByPostalCode.results[0]?.taxSales);
console.log('Cities:', ratesByPostalCode.results.map((r) => r.geoCity).join(', '));
console.log('---');
// Get account metrics
const metrics = await client.getAccountMetrics();
console.log('Account Metrics:');
console.log('Requests:', metrics.request_count, '/', metrics.request_limit);
console.log('Usage:', metrics.usage_percent.toFixed(2), '%');
console.log('Active:', metrics.is_active);
} catch (error) {
console.error('Error:', error);
}
}
main();