· 5 years ago · Oct 30, 2020, 04:22 PM
1# package used to execute HTTP POST request to the API
2import json
3import urllib.request
4
5# API Key
6TOKEN = '0f899ed36febf9ffc7b3c7b1f98b423bbed35b5f738c79e3f10da6efaa485741' # replace YOUR_API_KEY with the API key you got from sec-api.io after sign up
7# API endpoint
8API = "https://api.sec-api.io?token=" + TOKEN
9
10# define the filter parameters you want to send to the API
11payload = {
12 "query": { "query_string": { "query": "cik:320193 AND filedAt:{2016-01-01 TO 2016-12-31} AND formType:\"10-Q\"" } },
13 "from": "0",
14 "size": "10",
15 "sort": [{ "filedAt": { "order": "desc" } }]
16}
17
18# format your payload to JSON bytes
19jsondata = json.dumps(payload)
20jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
21
22# instantiate the request
23req = urllib.request.Request(API)
24
25# set the correct HTTP header: Content-Type = application/json
26req.add_header('Content-Type', 'application/json; charset=utf-8')
27# set the correct length of your request
28req.add_header('Content-Length', len(jsondataasbytes))
29
30# send the request to the API
31response = urllib.request.urlopen(req, jsondataasbytes)
32
33# read the response
34res_body = response.read()
35# transform the response into JSON
36filings = json.loads(res_body.decode("utf-8"))
37
38# print JSON
39print(filings)