Define in details about the types of Rest API ?

#api

Answer

Overview

REST (Representational State Transfer) APIs follow specific principles and can be categorized by their HTTP methods, resource design, and architectural patterns.


HTTP Method Types

MethodOperationDescription
GETReadRetrieve a resource
POSTCreateSubmit data to create a new resource
PUTReplaceReplace an entire resource
PATCHUpdatePartially update a resource
DELETEDeleteRemove a resource
HEADMetadataLike GET but returns only headers
OPTIONSCapabilitiesReturns allowed methods (CORS preflight)

REST API Maturity Levels (Richardson Model)

text
Level 0 — Plain Old XML / JSON over HTTP (one endpoint)
Level 1 — Resources (multiple endpoints: /users, /posts)
Level 2 — HTTP Verbs (GET, POST, PUT, DELETE properly used)
Level 3 — Hypermedia Controls (HATEOAS — responses include links)

Level 0 (One endpoint)

text
POST /api?action=getUser&id=1
POST /api?action=createUser

Level 2 (Proper REST)

text
GET    /users          → Get all users
GET    /users/1        → Get user 1
POST   /users          → Create user
PUT    /users/1        → Replace user 1
PATCH  /users/1        → Update part of user 1
DELETE /users/1        → Delete user 1

Level 3 (HATEOAS)

json
{
  "id": 1,
  "name": "Alice",
  "_links": {
    "self": {"href": "/users/1"},
    "orders": {"href": "/users/1/orders"},
    "delete": {"href": "/users/1", "method": "DELETE"}
  }
}

API Authentication Types

TypeDescriptionExample
No AuthPublic endpointPublic news API
API KeyStatic key in header
text
x-api-key: abc123
Bearer TokenJWT in Authorization header
text
Authorization: Bearer <token>
OAuth 2.0Delegated access (Google, Facebook)Social login
Basic AuthBase64 username:passwordInternal APIs

REST API Response Types

dart
// 1. Single resource
{"id": 1, "name": "Alice"}

// 2. Collection
[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]

// 3. Paginated
{
  "data": [...],
  "page": 1,
  "perPage": 20,
  "total": 150,
  "nextPage": "/users?page=2"
}

// 4. Error response
{"error": "NOT_FOUND", "message": "User not found", "statusCode": 404}

REST vs GraphQL vs gRPC

FeatureRESTGraphQLgRPC
ProtocolHTTPHTTPHTTP/2
Data formatJSONJSONProtobuf
FlexibilityFixed endpointsQuery exactly what you needContract-based
PerformanceMediumMediumHigh
Best forStandard CRUD APIsComplex UI data fetchingMicroservices

REST API Best Practices

text
✅ Use nouns for resources (/users, not /getUsers)
✅ Use HTTP methods correctly (GET to read, POST to create)
✅ Return appropriate HTTP status codes
✅ Use plural resource names (/users, /orders)
✅ Version your API (/v1/users, /v2/users)
✅ Use pagination for lists
✅ Use HTTPS always
❌ Never use verbs in endpoints (/getUser, /deletePost)

Standard REST patterns:

text
GET /resources
(list),
text
GET /resources/{id}
(single),
text
POST /resources
(create),
text
PUT /resources/{id}
(replace),
text
PATCH /resources/{id}
(update),
text
DELETE /resources/{id}
(delete).