Macia_Pro

1. What is an API?

The current image has no alternative text. The file name is: Capturejjj.jpg
  • API (Application Programming Interface): A way for applications to communicate with each other.
  • In Python, APIs are often accessed over the web using HTTP requests.
  • Data is usually returned in JSON format.

2. The requests Library

The most common library for working with APIs in Python.

Install if needed:

pip install requests

3. Making a GET Request

import requests

response = requests.get("https://api.github.com/users/octocat")

# Status code
print("Status:", response.status_code)

# Response as JSON
data = response.json()
print("Username:", data["login"])
print("Public Repos:", data["public_repos"])

GET requests are used to retrieve data.


4. Making a POST Request

import requests

url = "https://jsonplaceholder.typicode.com/posts"
payload = {
    "title": "My First Post",
    "body": "Hello, this is a test post.",
    "userId": 1
}

response = requests.post(url, json=payload)
print("Status:", response.status_code)
print("Response:", response.json())

POST requests are used to send data.


5. Handling Authentication

Some APIs require authentication (API key, token, etc.).

headers = {
    "Authorization": "Bearer YOUR_API_KEY"
}

response = requests.get("https://api.example.com/data", headers=headers)
print(response.json())

6. Common HTTP Status Codes
CodeMeaning
200Success
201Resource created
400Bad request
401Unauthorized (need API key/token)
403Forbidden
404Not found
500Server error

7. Example: Weather API
import requests

API_KEY = "your_api_key"
city = "London"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"

response = requests.get(url)
data = response.json()

print("City:", data["name"])
print("Temperature:", data["main"]["temp"], "°C")
print("Weather:", data["weather"][0]["description"])

8. Best Practices

  • Always check response.status_code before using data.
  • Handle errors with try/except.
  • Avoid exposing your API keys (use environment variables).
  • Respect API rate limits.

Summary

  • APIs allow Python to communicate with web services.
  • Use the requests library to make GET and POST requests.
  • Most APIs return JSON, which you can parse with .json().
  • Authentication (API keys, tokens) is often required.
  • Always handle status codes and possible errors.
⬇️ Download Macia_Pro تحميل Server 1

Related Posts

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more

Drag_Club

1. What is JSON? Example JSON data: 2. Working with JSON in Python Python has a built-in json module. 3. Converting JSON to Python (Parsing) 4. Converting Python to JSON…

Read more

BEASh

1. What Is a Module? A module is simply a Python file (.py) that contains functions, classes, or variables you can reuse in other programs. Example: mymodule.py Using it in…

Read more

Phonex_pro

1. Opening a File Python uses the built-in open() function: File Modes Mode Description “r” Read (default) – opens file for reading; error if file doesn’t exist “w” Write –…

Read more

Tigriptv

Introduction Errors are a normal part of programming. In Python, you will encounter two main types of errors: Python provides a powerful way to handle exceptions so your program doesn’t…

Read more

Cleo_patra

1. Introduction File handling is one of the most important skills in Python. Almost every real-world application needs to read from or write to a file. Python makes file handling…

Read more