top of page

API - basic concept

Writer: Tuan AnhTuan Anh


In this post, I'll introduce the definition and basic concepts related to APIs for those of you who are new to this topic.


1. What is an API?

API stands for Application Programming Interface. It's a set of definitions and protocols that allow different applications or service layers within the same system to communicate with each other.


Examples:

  • You use a weather app to check the forecast. This weather app uses an API to access weather data from another source, such as the National Weather Service website.

  • You shop online and pay with VNPay. VNPay uses an API to communicate with your bank to process the payment.


To put it simply, an API is like an interpreter that helps two people speaking different languages understand each other.



2. How does an API work?

APIs work based on requests and responses.

  • Request: When an application wants to access data or functionality from another application, it sends a request to that application's API.

  • Response: The API processes the request and sends a response back to the application. The response usually includes data or the result of the requested function.


3. Benefits of Using APIs

  • Reusability: APIs allow developers to reuse existing functionalities instead of writing them from scratch.

    • Example: Suppose you're building an online store. Instead of writing your own credit card payment processing function, you can use a payment API from a provider like Napas. This API provides all the necessary functions to handle payments securely and efficiently, saving you time and effort.

  • Application Integration: APIs enable different applications to connect and create integrated systems.

    • Example: Suppose you're building a scheduling app. You can use Google Calendar API to allow users to sync their calendars with your app. This makes it easier for users to manage their schedules from one place and improves the overall user experience.

  • Extensibility: APIs allow you to extend the functionality of an application by adding new services.

    • Example: Suppose you're building a map application. You can use the Google Maps API to add features like location search, directions, and satellite imagery to your app. This makes your application more useful and engaging for users.

  • Data Access: APIs allow access to data from another application.

    • Example: Suppose you're building a stock market analysis app. You can use APIs from stock data providers like Polygon or ISDB to retrieve stock price data.

  • Creating New Services: APIs can be used to create new services that can be utilized by other applications.

    • Example: Suppose you're building a translation app. You can use the Google Translate API to provide translation services to your users. This allows you to create a new valuable service for users and potentially monetize it.

  • In addition, APIs offer many other benefits such as:

    • Increased development speed: APIs help developers build applications faster and easier.

    • Reduced development costs: APIs help developers save on application development costs.

    • Improved user experience: APIs help create user-friendly and easy-to-use applications.

    • Innovation promotion: APIs help drive innovation in the software industry.

4. Common API Types

  • REST API: The most common type of API, using HTTP methods (GET, POST, PUT, DELETE) for data access.

  • SOAP API: A more traditional type of API that uses XML to define requests and responses.

  • GraphQL API: A newer type of API that allows more flexible data access.


4.1 REST API:

Characteristics:

  • Based on REST (Representational State Transfer) architecture – a stateless software architecture that uses resources and HTTP methods for data access.

  • Easy to use, learn, and implement.

  • Flexible and scalable.

  • Stateless, reducing server load.

  • Uses JSON or XML data format.

Examples:

  • Retrieve a list of products: GET /products

  • Add a new product: POST /products

  • Update a product: PUT /products/{id}

  • Delete a product: DELETE /products/{id}

Advantages:

  • Easy to use and learn.

  • Flexible and scalable.

  • Stateless, reducing server load.

  • Supported by many tools and libraries.

Disadvantages:

  • Can be challenging for accessing complex data.

  • Less control compared to other API types.


Playground: You can use Swagger's sample API collection to learn and experiment: https://petstore.swagger.io/


4.2 SOAP API:

Characteristics:

  • Based on SOAP (Simple Object Access Protocol) – an XML-based communication protocol for exchanging data and functionality between applications.

  • Complex structure, requiring more knowledge to use.

  • Often used for complex systems that require high security.

  • Uses XML data format.

Examples:

  • User authentication: AuthenticateUser(username, password)

  • Retrieve product information: GetProductDetails(productId)

  • Update order: UpdateOrder(orderId, items)

Advantages:

  • Clear structure, easy to identify components.

  • High security, supports many advanced security features.

  • Good compatibility with enterprise systems.

Disadvantages:

  • Complex and difficult to use.

  • Slower performance compared to other API types.

  • Less flexible and difficult to scale.



4.3 GraphQL API

Characteristics:

  • A newer type of API that uses GraphQL queries – a query language that allows flexible and efficient data access.

  • Allows accessing data on demand, minimizing unnecessary data downloads.

  • Flexible structure, easy to modify without affecting other applications.

  • Uses JSON data format.

Examples:

  • Retrieve product information and a list of reviews: { product(id: 123) { name, price, reviews { author, content } } }

  • Retrieve a list of products by category and price range: { products(category: "electronics", priceRange: { min: 100, max: 500 }) { id, name, price } }

Advantages:

  • Flexible and efficient data access.

  • Flexible structure, easy to modify.

  • Minimizes unnecessary data downloads.

  • Supported by a strong development community.

Disadvantages:

  • New technology, requires time to learn and implement.

  • Fewer tools and libraries compared to other API types.



I hope this overview of APIs has been helpful. Feel free to share your questions or experiences with APIs in the comments!

 
 

Comments


  • Facebook
  • LinkedIn

TankClass

bottom of page