Create API‘s
in Python
+91-7260058093
[Link]
What is API ?
API stands for application
programming interface.
It is a way for different
computer programs to
communicate with each
other.
How API WORKS?
We make a request for
information or data, and the API
returns a response with what
you requested.
Common Example
We can use an weather API to
request weather data for a
specific location, and it will
return a response with the
current weather information for
that location.
Python and APIs
When creating APIs with Python,
there's only one library you need:
requests.
You can easily send and receive data
from APIs with requests library.
pip install requests
Install requests library
Creating our first API
request
How about fetching a random joke
from the internet? For that, we'll use
the JokeAPI.
We will take url of api from internet.
Code for Joke Generator
import requests
# The API URL for random jokes
url = "[Link]
# Sending a GET request to the API
response = [Link](url)
# Checking if the request was successful (status code 200)
if response.status_code == 200:
#Extracting the joke from the response
joke = [Link]()
print(joke['joke'])
else:
print("Oops! Something went wrong.")
Understanding the code
We specify the URL of the JokeAPI
we want to request.
We use [Link](url) to send
a GET request to that URL.
We check if the response status
code is 200, which means the
request was successful.
If it's successful, we extract the
joke from the response and print
it.