Skip to content

Instantly share code, notes, and snippets.

@janlukasschroeder
Last active September 8, 2022 14:17
Show Gist options
  • Save janlukasschroeder/84dbd69e83800d0b2a073df754ebcf82 to your computer and use it in GitHub Desktop.
Save janlukasschroeder/84dbd69e83800d0b2a073df754ebcf82 to your computer and use it in GitHub Desktop.
sec-filings-api-notebook.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/janlukasschroeder/84dbd69e83800d0b2a073df754ebcf82/sec-filings-api-notebook.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yH3LHzT3ctyv"
},
"source": [
"# SEC EDGAR API "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2JSTHCL1ctyy"
},
"source": [
"In this notebook we explain how to use the SEC EDGAR API to access 10-Q filings of Apple filed in 2016.\n",
"\n",
"A detailed documentation is hosted here: https://sec-api.io/docs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gJZf8ti6ctyz",
"outputId": "858cfb6d-e698-4af6-dd4f-f3a633acf498"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'total': 3, 'filings': [{'cik': '320193', 'ticker': 'AAPL', 'companyName': 'APPLE INC', 'formType': '10-Q', 'filedAt': '2016-07-27', 'linkToTxt': 'https://www.sec.gov/Archives/edgar/data/320193/0001628280-16-017809.txt', 'linkToHtml': 'https://www.sec.gov/Archives/edgar/data/320193/0001628280-16-017809-index.htm', 'id': '73185d9f04406fb9aadf93bd9c55a0f1'}, {'cik': '320193', 'ticker': 'AAPL', 'companyName': 'APPLE INC', 'formType': '10-Q', 'filedAt': '2016-04-27', 'linkToTxt': 'https://www.sec.gov/Archives/edgar/data/320193/0001193125-16-559625.txt', 'linkToHtml': 'https://www.sec.gov/Archives/edgar/data/320193/0001193125-16-559625-index.htm', 'id': '2ba487ec411e2964a3b5d4aa1b209e2c'}, {'cik': '320193', 'ticker': 'AAPL', 'companyName': 'APPLE INC', 'formType': '10-Q', 'filedAt': '2016-01-27', 'linkToTxt': 'https://www.sec.gov/Archives/edgar/data/320193/0001193125-16-439878.txt', 'linkToHtml': 'https://www.sec.gov/Archives/edgar/data/320193/0001193125-16-439878-index.htm', 'id': '6ea11fe857964a75f4a4c8dbde9aad89'}]}\n"
]
}
],
"source": [
"##########################\n",
"# Python 3.x Example\n",
"##########################\n",
"\n",
"# package used to execute HTTP POST request to the API\n",
"import json\n",
"import urllib.request\n",
"\n",
"# Visit https://sec-api.io/signup/free to get your free key\n",
"API_KEY = \"YOUR_API_KEY\"\n",
"\n",
"# API endpoint\n",
"API = \"https://api.sec-api.io\"\n",
"\n",
"# define the filter parameters you want to send to the API \n",
"payload = {\n",
" \"query\": { \"query_string\": { \"query\": \"cik:320193 AND filedAt:{2016-01-01 TO 2016-12-31} AND formType:\\\"10-Q\\\"\" } },\n",
" \"from\": \"0\",\n",
" \"size\": \"10\",\n",
" \"sort\": [{ \"filedAt\": { \"order\": \"desc\" } }]\n",
"}\n",
"\n",
"# format your payload to JSON bytes\n",
"jsondata = json.dumps(payload)\n",
"jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes\n",
"\n",
"# instantiate the request \n",
"req = urllib.request.Request(API)\n",
"\n",
"# set the correct HTTP header: Content-Type = application/json\n",
"req.add_header('Content-Type', 'application/json; charset=utf-8')\n",
"# set the correct length of your request\n",
"req.add_header('Content-Length', len(jsondataasbytes))\n",
"# set your API key\n",
"req.add_header('Authorization', API_KEY)\n",
"\n",
"# send the request to the API\n",
"response = urllib.request.urlopen(req, jsondataasbytes)\n",
"\n",
"# read the response \n",
"res_body = response.read()\n",
"# transform the response into JSON\n",
"filings = json.loads(res_body.decode(\"utf-8\"))\n",
"\n",
"# print JSON \n",
"print(filings)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IXkI60sDcty2"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment