FMP

FMP

Enter

Financial Modeling Prep has a suite of APIs to enable you to access the most valuable financial data out there. But what is an API and how does it work? Simply,

How to call the Financial Modeling Prep API with Python

Sep 30, 2022 2:21 AM - Jack Dalton

twitterlinkedinfacebook
blog post cover photo

Image credit: Douglas Lopes

Financial Modeling Prep has a suite of APIs to enable you to access the most valuable financial data out there. But what is an API and how does it work? Simply, an API is a gateway for people to send and receive data over the internet. FMP has free stock APIs, Financial Statement APIs, and many more. In this article, we cover:

  1. What is an API?
  2. How do you make your first FMP API request with Python?

What is an API?

The commonly used abbreviation API stands for Application Programming Interface but what does that really mean. Simply put, API's are a way for developers, programmers, and analysts to get data from a source. Many companies use APIs as a way for users to access data from their own servers. This could be free for basic data or paid depending on the complexity and rarity of the data. API's are essentially a server that you can use to retrieve and send data using a code.

The basic process of how API's work to receive data is as follows:

  1. The user performs a ‘GET' request to the server
  2. The server responds with a response code and the data

FMP offers stock data, historical financial information, and financial statement data via our own API. This enables you access to the most comprehensive and useful financial information to perform your own analysis, build models to make informed data-driven investments, and create your financial applications.

How to Make Your First FMP API Request with Python?

The first step is to create an FMP account - this guidde will help you through it. Next, you'll need to make sure you have a Python environment to work in. This article will guide you through that if you haven't already made one. FMP has a multitude of financial information APIs including financial statements, stock data, cryptocurrency, FOREX, and more. So how do you get this information from the FMP free API? We're going to go through a demo by pulling from the Company Profile API.

Step 1: Load FMP Developer API Docs Website

From this article go to the top right of the page, click “Developers” and then click “API Docs” from the list below. Make sure you are logged into your account. You should see this in the top right corner:

image

Otherwise, click login and enter your account credentials.

A key piece of information that'll want to write down in a safely secured location is your API Key. This is located in the second section of the API docs page which is called “Your Details”. See in the example below, it should be a long series of letters and numbers. As with any key, this unlocks something and in this case it ensures that you have permission to access the API you are requesting. Make sure to keep your key private and safely secured if you are storing it somewhere on your computer. It is against our terms and conditions to share your API key with anyone - your account will be blocked and investigated if we suspect you are doing this.

Step 2: Copy the URL for the API you want to access

For this example, we are going to pull from the company profile API. So scroll down to the section that says Company Profile (You can also use the navigation bar on the left hand side of the screen) and you will see the following:

image

Then right click on the box that contains something that looks like a condensed URL. In this case: api/v3/profile/APPL

image

Step 3: Open Your Python Environment and copy URL

Now you'll need to open up your Python environment to start writing code. To start with, create a variable called “URL” and give the string version of the URL you copied in step 2. You can simply use Ctrl + V (or cmd + V for mac users) to paste the url you copied.

ERROR ALERT: Make sure to add inverted commas (otherwise called quotation marks) around the copied URL to make the URL variable a string.

url = "https://financialmodelingprep.com/api/v3/profile/AAPL?apikey=demo"

If you're copying directly from this page you'll need to make sure that you replace the ‘demo' with your API key

Step 4: Import JSON and add try/except block

Now we're going to use some code that you don't have to fully understand in order to use. One of the beautiful things about data analysis using Python is that it is an open source programming language so there is lots of code on the internet that you can use for free!

First, we need to import the JSON library. JSON is a data format often used by API creators to send their data to people accessing their API. The JSON library is a pre-made set of functions created to help people easily use the JSON format. By importing this library, you will then have access to all the JSON library functions. To import JSON add this to your Jupyter notebook cell

import json

Now we are going to copy in what is called a try/except block. A block of code is a segment of code designed with a specific purpose. With try/except, when the program runs it will first attempt to do what is after the try: statement and if it encounters the specific error after the except block it will run that code instead. In this case, we are telling the computer to first try using Python version 3 and if there is an ImportError then use Python version 2.

try: # For Python 3.0 and later from urllib.request import urlopen except ImportError: # Fall back to Python 2's urllib2 from urllib2 import urlopen

Step 5: Add function to return the API data in a usable format

Now we're going to add a function that can be recycled later to easily return the API contents in a usable form. The function is called get_jsonparsed_data because it takes the url you're trying to access, parses the JSON data, and returns the data in the form of a list. Here's what it looks like:

def get_jsonparsed_data(url): """ Receive the content of ``url``, parse it as JSON and return the object. Parameters ---------- url : str Returns ------- dict """ response = urlopen(url) data = response.read().decode("utf-8") return json.loads(data)

We won't delve into what each part means but it's important to know how to use this function and what you get out of it. The bracketed item after get_jsonparsed_data(url), url in this case, is what you need to put into the function. So when you use the function later you'll need to make sure that you have either defined url or you copy the url directly into that space eg:

url = "https://financialmodelingprep.com/api/v3/profile/AAPL?apikey=demo" get_jsonparsed_data(url)

OR

get_jsonparsed_data("https://financialmodelingprep.com/api/v3/profile/AAPL?apikey=demo")

Either of these options will work to call the url. We recommend the first option as it will make it easier to easily change the url for different stocks and APIs.

Step 6: Call the function and receive the data!

The final step before hitting the enter button and running the cell is to copy the function down onto the last line of the cell. You can do this using either option described above. The final product of code in the cell should look like this:

try: # For Python 3.0 and later from urllib.request import urlopen except ImportError: # Fall back to Python 2's urllib2 from urllib2 import urlopen import json def get_jsonparsed_data(url): """ Receive the content of ``url``, parse it as JSON and return the object. Parameters ---------- url : str Returns ------- dict """ response = urlopen(url) data = response.read().decode("utf-8") return json.loads(data) url = ("https://financialmodelingprep.com/api/v3/profile/AAPL?apikey=demo") get_jsonparsed_data(url)

When you enter the output should look like this and as you can see it should have a type that is a list.

image image

There you go! You have successfully pulled data from the FMP API! If you're an absolute novice to coding, this will be interesting but not particularly useful. To learn more about how to perform analysis using python and the FMP API, be sure to check out our other How to articles!

Other Blogs

Sep 11, 2023 1:38 PM - Rajnish Katharotiya

P/E Ratios Using Normalized Earnings

Price to Earnings is one of the key metrics use to value companies using multiples. The P/E ratio and other multiples are relative valuation metrics and they cannot be looked at in isolation. One of the problems with the P/E metric is the fact that if we are in the peak of a business cycle, earni...

blog post title

Sep 11, 2023 1:49 PM - Rajnish Katharotiya

What is Price To Earnings Ratio and How to Calculate it using Python

Price-to-Earnings ratio is a relative valuation tool. It is used by investors to find great companies at low prices. In this post, we will build a Python script to calculate Price Earnings Ratio for comparable companies. Photo by Skitterphoto on Pexels Price Earnings Ratio and Comparable Compa...

blog post title

Oct 17, 2023 3:09 PM - Davit Kirakosyan

VMware Stock Drops 12% as China May Hold Up the Broadcom Acquisition

Shares of VMware (NYSE:VMW) witnessed a sharp drop of 12% intra-day today due to rising concerns about China's review of the company's significant sale deal to Broadcom. Consequently, Broadcom's shares also saw a dip of around 4%. Even though there aren’t any apparent problems with the proposed solu...

blog post title
FMP

FMP

Financial Modeling Prep API provides real time stock price, company financial statements, major index prices, stock historical data, forex real time rate and cryptocurrencies. Financial Modeling Prep stock price API is in real time, the company reports can be found in quarter or annual format, and goes back 30 years in history.
twitterlinkedinfacebookinstagram
2017-2024 © Financial Modeling Prep