What the heck is API?!

What the heck is API?!

Guide to APIs for Beginners

·

4 min read

Roll up your sleeves; you're about to learn about APIs. you might have heard this three-letter word. Well, let me break it down for you.

API?

let's think of API as a vending machine

you press the button (API calls ) and insert the money(provide any required data ).The vending machine then dispenses the chosen item (function output) if everything is selected correctly.

An Application Programming Interface (API) is a contract that allows code to talk to other code. APIs are the building blocks of modern software because they allow for sharing of resources and services across applications, organizations, and devices.

Types of API

We can divide API into three different sections /types

  1. Medium
  • Hardware APIs
    Interface for software to talk to hardware.

  • Software Library APIs
    Interface for directly consuming code from another code base.

  • Web APIs
    Interface for communicating across code bases over a network.

  1. Architecture

    There is more than one way to build API

    • REST (Representational State Transfer)

    • GraphQL

    • WebSockets

    • webhooks

    • SOAP (Simple Object Access Protocol)

    • gRPC (Google Remote Procedure Call)

    • MQTT (MQ Telemetry Transport)

      The most famous and used is Rest API

  2. Access

    APIs also vary in the scope of who can access them.

    ->Public APIs (aka Open APIs)
    Consumed by anyone who discovers the API

    ->Private APIs
    Consumed only within an organization and not made public

    ->Partner APIs
    Consumed between one or more organizations that have an established relationship

Request Methods

when we press the button to specify on the vending machine what we want, the same as when we specify a request method that indicates the type of operation we are about to perform. These are also called HTTP verbs.

Some common HTTP request methods correspond to the CRUD operations mentioned earlier.

Method nameOperation
GETRetrieve data (Read)
POSTSend data (Create)
PUT/PATCHUpdate data (Update)

PUT usually replaces an entire resource, whereas PATCH usually is for partial updates DELETE Delete data (Delete)

Examples

GET : As it is used to retrieve data, it will fetch the user profile, listing items

POST: it will create a new user profile or submit the form

PUT: used for complete replacement of the user's profile of an existing resource. it updates the entire profile of the user

PATCH: used for partial updates to an existing resource. it will update the update the user email without changing the other fields

Request URL

To request a method, you must specify the URL to know where to make API calls.

Request URL has three parts

  • protocol (such as http:// or https://)

  • host (location of the server)

  • and path (route on the server).

Response status codes

status code specifies whether the request has failed or succeeded.

status code has conventions and here they are listed below

Code rangeMeaningExample
2xxSuccess200 - OK
201 - Created
204 - No content (silent OK)
3xxRedirection301 - Moved (path changed)
4xxClient error400 - Bad request
401 - Unauthorized
403 - Not Permitted
404 - Not Found
5xxServer error500 - Internal server error
502 - Bad gateway
504 - Gateway timeout

Query parameters

Remember that the minimum ingredients you need to make a request are:

  • a request method (GET/POST/PUT/PATCH/DELETE, etc)

  • a request URL

some APIs allow to refinement the request further with key-value pairs called query parameters

Search Google - with query parameters

https://www.google.com/search?q=hashnode

This request adds a search term as a query parameter q=hashnode ("q" refers to "query" here) to the GET /search path on Google's server.

Path Variable

Another way of passing request data to an API is via path variables (a.k.a. "path parameters"). A path variable is a dynamic section of a path and is often used for IDs and entity names such as usernames.

Note that some API documentation uses colon syntax to represent a wildcard in the path like /users/:username, while some use curly braces like /users/{username}. They both mean the same thing: that part of the path is dynamic!

Path Variable syntax

The path variable comes immediately after a slash in the path.

ex: https://api.github.com/users/{username}

calling this API with a value {username} will fetch data about that user

you can have multiple path variables in a single request


Thanks for reading this article! If you have any feedback or questions, drop them in the comments and I'll get back to you.

connect with me on Twitter, Linkedin, and Hashnode. Let's stay in touch :)!!