API Fundamentals Crash Course
API
An API (Application Programming Interface) acts as a standardized intermediary. It
allows two different software programs to connect and talk to each other without
needing to understand each other's underlying code. [1]
Endpoints
An endpoint is a specific web address (URL) where a client interacts with a designated
resource on the server. [1, 2]
text
None
[Link]
Use code with caution.
Methods
HTTP methods specify the type of action you want to execute on a given resource
endpoint: [1, 2]
● GET: Retrieves data.
● POST: Creates a new resource.
● PUT: Overwrites or completely replaces a resource.
● PATCH: Partially modifies a resource.
● DELETE: Removes a resource. [1, 2, 3]
Request & Response
● Request: The package of information sent by the client to the server (containing
the method, URL, headers, and optional body payload).
● Response: The package returned by the server containing a status code,
headers, and the requested data layout. [1, 2, 3]
Status Code
A status code is a 3-digit number sent by the server summarizing the outcome of the
request: [1, 2]
● 200 OK: Request succeeded.
● 201 Created: Resource successfully generated.
● 400 Bad Request: Client input error.
● 401 Unauthorized: Authentication missing or invalid.
● 404 Not Found: Resource cannot be located.
● 500 Internal Server Error: Server-side bug. [1, 2, 3, 4, 5, 6]
Headers
Headers pass essential metadata alongside requests and responses to dictate
connection rules, body types, or tracking data. [1, 2]
http
None
Content-Type: application/json
Accept: application/json
Use code with caution.
Authentication
Authentication securely verifies the identity of the client attempting to access protected
resources. The most common approach uses an Authorization header containing a
secure credential token. [1, 2, 3]
http
None
Authorization: Bearer confidential_api_token_xyz123
Use code with caution.
Data Format
The structural arrangement used to transmit the actual data payload back and forth.
JSON (JavaScript Object Notation) is the industry standard for REST APIs due to its
readability. [1, 2]
json
None
{
"id": 101,
"username": "dev_user",
"status": "active"
}
Use code with caution.
Parameters
Variables sent along with a request to filter, sort, or specify data details: [1, 2]
● Query Parameters: Appended to the end of a URL string following a ? symbol.
● Path Parameters: Embedded directly within the URL path structure.
text
None
[Link]
└───┘ └────┘
Path Parameter Query Parameter
Use code with caution.
Error Handling
The intentional design of structural error payloads so a client app can programmatically
understand a failure and resolve it. [1]
json
None
{
"status": 404,
"error": "Not Found",
"message": "The user with ID 101 does not exist in our database."
}
Use code with caution.
Comprehensive Python Integration Code Sample
This comprehensive script demonstrates how all of these components work together
inside a practical application context using the Python requests library:
python
None
import requests
# 1. Base URL config and target resource endpoint
base_url = "[Link]
endpoint = f"{base_url}/users"
# 2. Setup Headers and Authentication
request_headers = {
"Content-Type": "application/json", # Data Format
declaration
"Authorization": "Bearer standard_mock_token" # Authentication
credential
}
# 3. Setup Parameters & Payload Data
query_params = {"status": "active"} # Query Parameters
new_user_payload = { # JSON data body
format
"username": "alex_code",
"role": "developer"
}
try:
# 4. Sending a POST Request Method
response = [Link](
endpoint,
json=new_user_payload,
headers=request_headers,
params=query_params
)
# 5. Reviewing the Response & Status Code
print(f"Status Code Returned: {response.status_code}")
if response.status_code == 201:
# Successful creation response
user_data = [Link]()
print("Success! Created User:", user_data["username"])
elif response.status_code == 401:
print("Error: Invalid or missing authentication credentials.")
else:
# Basic API Error Handling block
print(f"Failed with payload: {[Link]}")
except [Link] as error:
# Code-level exception safety block
print(f"Network or structural system error: {error}")
Use code with caution.