Introduction to APIs: REST vs RPC

πŸ’» Speak the language of software communication

An API (Application Programming Interface) is a set of rules and protocols that allows different software systems to talk to each other. APIs define how requests are made, what data is exchanged, and how responses are structured. Whether you’re building web apps, mobile apps, or backend services, APIs are the bridge that connects everything.

In this guide, you’ll learn what APIs are, the two popular styles β€” REST and RPC β€” and how they differ in design and usage.


πŸ“ What is an API Used For?

  • Connecting frontend applications to backend servers
  • Accessing data stored remotely
  • Integrating third-party services (payment gateways, social media, maps)
  • Enabling modular, reusable software components
  • Automating workflows between software systems

🧱 API Basics

APIs allow a client (your app) to send requests to a server and receive responses. Requests can ask for data, create new entries, update information, or delete records.

Example: When you order a taxi via an app, the app’s API sends your location and request to the server, which finds a driver and returns confirmation.


πŸ”Ή Two Popular API Styles

StyleWhat it Focuses OnHow it WorksTypical Data Format
RESTResources (data entities)Uses HTTP methods (GET, POST, PUT, DELETE) on URLs representing resourcesJSON, XML
RPCActions/Procedures (methods/functions)Calls methods directly with parametersJSON-RPC, XML-RPC

REST (Representational State Transfer)

🌐 Resource-oriented, HTTP-based design

REST treats everything as a resource identified by a URL. Clients use standard HTTP verbs to perform actions:

HTTP VerbActionExample
GETRetrieve dataGET /users/123 (get user 123)
POSTCreate new resourcePOST /users (create new user)
PUTUpdate existing resourcePUT /users/123 (update user 123)
DELETERemove resourceDELETE /users/123 (delete user 123)

Key Characteristics:

  • Stateless: Each request contains all necessary info; no session info on server.
  • Uniform interface: Clear, standardized operations.
  • Scalable and cacheable.
  • Ideal for CRUD (Create, Read, Update, Delete) operations.

RPC (Remote Procedure Call)

βš™οΈ Action-oriented, procedure call style

RPC APIs focus on calling methods or functions on the server, passing parameters and getting results.

Example RPC CallMeaning
createUser(name: "Alice", email: "a@example.com")Call method to create user
deleteUser(id: 123)Call method to delete user

Key Characteristics:

  • Emulates local procedure/function calls over network.
  • May use JSON-RPC, XML-RPC, or custom protocols.
  • Can be simpler for complex operations.
  • Not necessarily tied to HTTP methods or resources.

πŸ“Œ Summary Table

FeatureRESTRPC
FocusResources (data)Methods (actions)
URLRepresents resources (nouns)Represents method calls (verbs)
HTTP MethodsUses GET, POST, PUT, DELETETypically single POST with method name and params
StateStatelessUsually stateless
Data FormatJSON, XML (resource representations)JSON-RPC, XML-RPC, or others
Use CasesCRUD APIs, web servicesComplex workflows, remote commands

🧠 Best Practices

βœ… Use REST when your API primarily exposes data entities with standard CRUD operations
βœ… Use RPC when your API needs to perform complex operations or commands not fitting the CRUD model
βœ… Keep APIs consistent and well-documented
βœ… Use meaningful URL/resource names in REST
βœ… Clearly define method names and parameters in RPC


πŸ“š Example Requests

REST Example:

GET /books/42 HTTP/1.1
Host: api.example.com
Accept: application/json

Response:

{
"id": 42,
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams"
}

RPC Example (JSON-RPC):

{
"jsonrpc": "2.0",
"method": "getBook",
"params": { "id": 42 },
"id": 1
}

Response:

{
"jsonrpc": "2.0",
"result": {
"id": 42,
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams"
},
"id": 1
}

πŸ”œ What’s Next?

APIs power modern web and mobile apps. Mastering REST and RPC will help you design efficient, scalable services and integrate third-party systems. Try building simple REST and RPC APIs to solidify your understanding!

Join us on the Web API Course