Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære What is an API? | API Basics and Making Requests
Working with APIs in Python

bookWhat is an API?

APIs, or Application Programming Interfaces, are essential tools that allow different software systems to communicate with each other. You interact with APIs every day—when you check the weather on your phone, view social media feeds, or look up the latest news, your apps are using APIs to fetch that information from remote servers. For Python developers, APIs are incredibly important because they enable your programs to connect with external data sources and services, making your applications more dynamic and powerful. For example, you might use an API to get live stock prices, retrieve weather forecasts, or access a database of interesting facts.

1234
import requests response = requests.get("https://catfact.ninja/fact") print(response.text)
copy

In the code above, you first import the requests library, which is a popular tool for making HTTP requests in Python. The requests.get() function sends a GET request to the Cat Facts API at https://catfact.ninja/fact. This request asks the server to send back a random cat fact. The response from the server is stored in the response variable, and response.text contains the raw text of the server's reply, which is printed to the console.

123456789
import requests response = requests.get("https://catfact.ninja/fact") if response.status_code == 200: print("Success! Here is your cat fact:") print(response.json()["fact"]) else: print(f"Error: Received status code {response.status_code}")
copy

HTTP status codes are numbers that tell you whether your API request was successful or if something went wrong. A status code of 200 means "OK"—your request worked and the server sent back the data you asked for. Other codes, like 404 ("Not Found") or 500 ("Server Error"), indicate problems. When working with APIs, always check the status code to make sure your request succeeded before trying to use the data in the response.

1. What does API stand for?

2. Which HTTP method is commonly used to retrieve data from an API?

question mark

What does API stand for?

Select the correct answer

question mark

Which HTTP method is commonly used to retrieve data from an API?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookWhat is an API?

Sveip for å vise menyen

APIs, or Application Programming Interfaces, are essential tools that allow different software systems to communicate with each other. You interact with APIs every day—when you check the weather on your phone, view social media feeds, or look up the latest news, your apps are using APIs to fetch that information from remote servers. For Python developers, APIs are incredibly important because they enable your programs to connect with external data sources and services, making your applications more dynamic and powerful. For example, you might use an API to get live stock prices, retrieve weather forecasts, or access a database of interesting facts.

1234
import requests response = requests.get("https://catfact.ninja/fact") print(response.text)
copy

In the code above, you first import the requests library, which is a popular tool for making HTTP requests in Python. The requests.get() function sends a GET request to the Cat Facts API at https://catfact.ninja/fact. This request asks the server to send back a random cat fact. The response from the server is stored in the response variable, and response.text contains the raw text of the server's reply, which is printed to the console.

123456789
import requests response = requests.get("https://catfact.ninja/fact") if response.status_code == 200: print("Success! Here is your cat fact:") print(response.json()["fact"]) else: print(f"Error: Received status code {response.status_code}")
copy

HTTP status codes are numbers that tell you whether your API request was successful or if something went wrong. A status code of 200 means "OK"—your request worked and the server sent back the data you asked for. Other codes, like 404 ("Not Found") or 500 ("Server Error"), indicate problems. When working with APIs, always check the status code to make sure your request succeeded before trying to use the data in the response.

1. What does API stand for?

2. Which HTTP method is commonly used to retrieve data from an API?

question mark

What does API stand for?

Select the correct answer

question mark

Which HTTP method is commonly used to retrieve data from an API?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt