Everything to get you started with creating programs using Financial APIs
Out-of-Date Toy Datasets | Real Financial APIs | |
---|---|---|
Data Freshness | Data is static and may be outdated. Requires manual updates. | Data is real-time and continuously updated from live sources. |
Relevance | May not reflect current market conditions or recent events. | Provides up-to-date information reflecting current market conditions. |
Data Volume | Limited to the data available at the time of download. | Can access large volumes of data and historical data if needed. |
Automation | Requires manual updates and reprocessing to stay current. | Data is automatically updated via API requests, reducing manual work. |
Application in Real-World Scenarios | Limited to predefined datasets with value masking or other pre-cleaned processing. Often outdated. | Direct application of data in real-time scenarios, useful for financial analysis and decision-making. |
Value for End Users | Applications may become outdated quickly, reducing their value. | Applications remain valuable with current, real-time insights and data. |
Market Data and Trading APIs
Financial News and Research APIs
Economic and Alternative Data APIs
python_0.py
.os
, requests
, and dotenv
..env
. This file will store your API key..env
file..gitignore
. This file will prevent your .env
file from being pushed to your repository.python_0.py
), you’ll write a function that retrieves financial data
from the external API using requests
.
1. Importing modules
import os
: This module provides a way to interact with the operating system, including accessing environment variables.import requests
: This module allows us to send HTTP requests, which is useful for interacting with web APIs.from dotenv import load_dotenv
: This imports the load_dotenv function from the dotenv module, which helps load environment variables from a .env file into the program.2. Retrieve our API Key
load_dotenv()
: This function loads the environment variables from the .env file into the program.SECTORS_API_KEY = os.getenv("SECTORS_API_KEY")
: This retrieves the value of the SECTORS_API_KEY environment variable from the .env file.3. Defining our retriever function
def get_info()
: This function sends a GET request to the Sectors API to retrieve financial data for BBRI.
A function is a reusable block of code that performs a specific task.url = f"https://api.sectors.app/v1/company/report/BBRI/"
: This is the URL of the API endpoint we want to access.response = requests.get(url, headers={"Authorization": SECTORS_API_KEY})
:
This sends a GET request to the API endpoint with the Authorization header containing our API key.return response.json()
: This returns the JSON response from the API.4. Calling the function!
response = get_info()
: This calls the get_info function to retrieve the financial data for BBRI.print(response)
: This prints the JSON response from the API to the console.SECTORS_API_KEY
with your actual API key.
If you’ve saved the script as python_0.py
, you can run it from the command line using the following command:
get_info
for flexibilitystock
symbol and the section
of the report
we want to retrieve.
This encourages code reusability and makes the function more flexible. The alternative
of creating multiple functions for different stocks or sections would be inefficient,
lead to code duplication, and make maintenance more challenging.
get_info
function, which accepts stock
and section
parameters:
headers
definition outside the function to make it a global variable.get_info
function now accepts stock
and section
as parameters.try-except
block to handle exceptions that may occur during the API request.get_info
function with different stock symbols and sections to retrieve the desired data.
python_0.py
with a few different combinations of stock
and section
parameters to see how the function behaves.
get_info
function:
get_info
function:
overall
to the section
parameter to make it optional:
section
parameter optional. If no value is provided, the function will retrieve all available sections by default.
The function can now be called with just the stock
parameter:
stock
parameter is a valid stock symbolsection
parameter is a valid section, according to the API documentationstock
symbol and the validity of the section
parameter.
We’ve also made the section
parameter optional by providing a default value of overview
. This allows users to call the function without specifying the section, in which case the overview section will be retrieved by default.
Run the exercises above in your own code editor and experiment with different stock symbols and sections to see how the function behaves.
.env
file to store your API key. This way, you can keep your API key secure
and prevent it from being exposed in your code.To use the .env
file, you need to install the python-dotenv
package. You can install it using
pip install python-dotenv
. This file should also be added to your .gitignore
file to prevent
it from being pushed to your repository.Feature | Explanation |
---|---|
Docstrings | Docstrings provide clear explanations of what functions do, including their parameters and return values. This is essential for understanding and using financial APIs correctly and efficiently. |
Type Hints | Type hints specify the expected data types for function inputs and outputs, reducing errors and improving code clarity. They help ensure data consistency when working with complex financial data. |
Assertions | Assertions validate that inputs meet expected criteria before processing. They help catch mistakes early, such as ensuring a stock symbol is the correct length, which is crucial for accurate API requests. |
Error Handling | Error handling ensures your code can manage unexpected issues like network errors or invalid API responses gracefully. This improves reliability and provides useful feedback when something goes wrong. |