0% found this document useful (0 votes)
3 views8 pages

Create API's in Python

The document explains what an API is and how it facilitates communication between computer programs. It provides an example of using a weather API and demonstrates how to create a simple joke generator API using Python's requests library. The document includes code for fetching a random joke from the JokeAPI and explains the code's functionality.

Uploaded by

eduardo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Create API's in Python

The document explains what an API is and how it facilitates communication between computer programs. It provides an example of using a weather API and demonstrates how to create a simple joke generator API using Python's requests library. The document includes code for fetching a random joke from the JokeAPI and explains the code's functionality.

Uploaded by

eduardo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like