DEV Community

avery
avery

Posted on

24. Application Programming Interfaces (APIs)

BootCamp by Dr.Angela

1. Introduction to APIs

  • What is an API?
    • API (Application Programming Interface) acts as a bridge that allows different software applications to communicate with each other.
    • APIs enable programs to exchange data and functionality without exposing internal implementation details.
  • REST APIs
    • REST (Representational State Transfer) is a common architectural style for web APIs.
    • REST APIs typically use the HTTP protocol.
  • Common HTTP Methods : GET(Retrieve data), POST(Create data), PUT(Replace data), PATCH(Partially update data), DELETE(Remove data)

2. Structuring API Requests

  • API Types
    • Private API : Frontend ↔ Your Server, Used internally within your application
    • Public API : Your Server ↔ External Server, Provided by third-party services
  • API Documentation : API documentation explains available endpoints, parameters, authentication methods, and response formats.
  • API Endpoints : An endpoint is a specific URL that provides access to a resource.
    • Base URL + Endpoint
    • ex) /random, /filter
  • Query Parameters : Used to filter, sort, or customize responses.
  • Path Parameters : Used to identify a specific resource.

3. What is JSON?

  • JSON (JavaScript Object Notation) : Lightweight format for storing and exchanging data, Human-readable and language-independent
  • JSON vs JavaScript Objects
    • JSON : Stored as text (string) { "name": "John", "age": 25 }
    • JavaScript Object : Actual JavaScript object { name: "John", age: 25 }
  • JSON Tools
  • Convert JSON to JavaScript Object : const data = JSON.parse(jsonData);
  • Convert JavaScript Object to JSON : const jsonData = JSON.stringify(data);

4. Making Server-Side API Requests with Axios

  • Axios : Popular HTTP client for JavaScript and Node.js, Simpler syntax than the native https module
  • Async/Await : Used to handle asynchronous API requests more cleanly.
    • ex) const response = await axios.get(url);
  • Basic GET Request : `import axios from "axios";

const response = await axios.get(url);
console.log(response.data);`

5. API Authentication

  • Why Authentication? : Authentication helps control access to APIs and protects resources from unauthorized use.
  • Common Authentication Methods
    • No Authentication : Public access, Usually rate-limited
    • Basic Authentication : Username + Password, Credentials are encoded using Base64, Typically sent in HTTP headers
    • API Key Authentication : API Key → API, Unique key issued by the API provider, Often included in headers or query parameters
    • Token-Based Authentication : Username/Password → Token → API, User logs in once, Server returns a token, Future requests use the token
  • OAuth : Delegated authorization protocol , Allows users to grant limited access without sharing passwords
    • Commonly used with : Google, GitHub, Facebook

6. REST APIs in Practice

Axios simplifies server-side API requests.
Authentication methods include Basic Auth, API Keys, Tokens, and OAuth.

Top comments (0)