PoliAPI Code Samples
Node.js
index.js
1 const callPoliAPI = async () => {
2 // query that will be used for this API call (see documentation for all queries)
3 const query = 'published_within';
4
5 // query parameter
6 const num_weeks = '2';
7
8 try {
9 // fetch all trades that have been published within the last two weeks
10 const response = await fetch(`https://api.poliapi.com/api/${query}/${num_weeks}`);
11
12 // error check the response of the API
13 if (!response.ok) {
14 throw new Error(`HTTP error! status: ${response.status}`);
15 }
16
17 // await the data and print it to the console
18 const result = await response.json();
19 console.log('API Response:', result);
20
21 // catch any errors with fetching
22 } catch (error) {
23 console.error('Error fetching data:', error);
24 }
25 }
Python
main.py
1 # pip install requests if not already done
2 import requests
3
4 # select a query
5 query = 'politician'
6
7 # select a query parameter
8 politician = 'Nancy Pelosi'
9
10 # format API call
11 query = requests.get(f'https://api.poliapi.com/api/{query}/{politician}')
12
13 # check for status of the API request
14 if response.status_code != 200:
15 print(f'Failed to get politician: status_code {response.status_code}')
16
17 # upon success print the trades as a json
18 else:
19 print(f'{politician} trades:', response.json())