0% found this document useful (0 votes)
2 views5 pages

Python Notes Set 10

The document provides an introduction to APIs, explaining their function as interfaces for applications to communicate. It covers REST APIs, how to make GET and POST requests using the requests library, and building an API with Flask. Additionally, it discusses authentication methods and error handling best practices in APIs.

Uploaded by

fake786king
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)
2 views5 pages

Python Notes Set 10

The document provides an introduction to APIs, explaining their function as interfaces for applications to communicate. It covers REST APIs, how to make GET and POST requests using the requests library, and building an API with Flask. Additionally, it discusses authentication methods and error handling best practices in APIs.

Uploaded by

fake786king
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
You are on page 1/ 5

Page 1: Introduction to APIs

What is an API?

- Interface for apps to talk to each other

REST API:

- Uses HTTP methods: GET, POST, PUT, DELETE

Response format: usually JSON


Page 2: Requests Library

import requests

GET Request:

response = requests.get("http://api.com/data")

print(response.json())

POST Request:

data = {"name": "Alice"}

response = requests.post("http://api.com", json=data)


Page 3: Building API with Flask

from flask import Flask, jsonify

@app.route('/api')

def api():

return jsonify({"message": "Hello API"})


Page 4: Authentication

API Key:

Send via headers or query param

Bearer Token:

Authorization: Bearer <token>

OAuth2 and JWT are also common methods


Page 5: Error Handling in APIs

Use try-except blocks

Return proper HTTP status codes:

- 200 OK

- 400 Bad Request

- 404 Not Found

- 500 Internal Server Error

Use jsonify to return error messages

You might also like