API vs Webpage — Key Differences Every Developer Must Know
In this tutorial, you will learn about API vs Webpage. We cover key concepts, practical examples, and best practices to help you master this topic.
The fundamental difference between an API and a webpage is that APIs return structured data (JSON, XML) for programmatic consumption, while webpages return HTML rendered by browsers for human reading.
What You'll Learn
- How to distinguish API endpoints from regular URLs
- Why APIs and webpages use different response formats
- When to use an API vs serving a webpage
Why It Matters
Building a modern application means knowing when to serve an HTML page and when to expose an API endpoint. Mixing them up creates confusing, hard-to-maintain systems.
flowchart LR
A["Browser requests /users"] --> B{"Server checks\nAccept header"}
B -->|"text/html"| C["Returns HTML page"]
B -->|"application/json"| D["Returns JSON data"]
C --> E["Browser renders\nhuman-readable page"]
D --> F["Client app parses\nstructured data"]
style B fill:#dbeafe,stroke:#2563eb
Real-World Use
When you visit https://api.github.com/users/octocat, you get JSON. When you visit https://github.com/octocat, you get an HTML page. Same server, different endpoints serving different consumers.
Code Examples
import requests
# API endpoint returns JSON
api_response = requests.get("https://api.github.com/users/octocat")
print(type(api_response.json())) # dict
# Regular webpage returns HTML
web_response = requests.get("https://github.com/octocat")
print(web_response.text[:200]) # <!DOCTYPE html>...
Expected output:
<class 'dict'>
<!DOCTYPE html>
# Compare content types
curl -s -o /dev/null -w "%{content_type}" https://api.github.com/users/octocat
echo ""
curl -s -o /dev/null -w "%{content_type}" https://github.com/octocat
Expected output:
application/json; charset=utf-8
text/html; charset=utf-8
Common Mistakes
1. Returning HTML from an API Endpoint
APIs should return JSON/XML, not HTML. HTML requires parsing and has no structure guarantees.
2. Treating Every URL as an API
Some URLs serve human-readable pages. Check the Accept header to distinguish.
3. Building APIs That Only Work in Browsers
APIs must work for any HTTP client, not just browsers with JavaScript.
4. Ignoring Content Negotiation
The same URL can serve different formats based on the Accept header.
5. Mixing API and Web Routes
Keep /api/ paths separate from web routes for clarity and security.
Practice Questions
- What format do most web APIs return?
- How does a server know whether to return HTML or JSON?
- Can the same URL serve both HTML and JSON?
- Why is it bad to return HTML from an API endpoint?
- What header tells the server what format the client expects?
Answers:
- JSON (JavaScript Object Notation).
- By checking the
Acceptheader in the request. - Yes, through a technique called content negotiation.
- Clients would need to parse HTML to extract data, which is fragile and slow.
- The
Acceptheader specifies the desired response format.
Challenge: Create a simple Flask app with one route that returns JSON when Accept: application/json and HTML when Accept: text/html.
FAQ
What's Next
Learn about API Protocols including HTTP, gRPC, and GraphQL, then explore REST Introduction for the most popular API style.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro