API Development & Wikidata API - bounswe/bounswe2024group10 GitHub Wiki

API Use and Development

API stands for Application Programming Interface. It is a set of rules and protocols that allow one software application to interact with another.

APIs play a crucial role in enabling isolated systems and software to work together.

Types of APIs

  1. RESTful APIs:

    REST stands for Representational State Transfer. It uses standard HTTP methods such as GET, POST, DELETE, etc. Currently, RESTful APIs are the most popular type for web and mobile applications due to their simplicity and scalability.

  2. GraphQL APIs:

    GraphQL provides clients with the option to retrieve only the data they need, offering more flexibility than REST APIs. It is also more efficient and reduces over-fetching.

  3. Authentication APIs:

    Authentication APIs simplify the authentication and authorization process for applications.

  4. Payment APIs:

    APIs that provide a set of functions and methods for online payments. Developers can integrate online payment systems into their mobile or web applications. Examples include Stripe and PayPal APIs.

To utilize a REST Api, we should send request to server which serves the API we want to use. Let us dive deeper to api request:

REST API Request

A REST (Representational State Transfer) API request is typically composed of several componenst. Here are the key components of a REST API request:

  1. Endpoint (URL):

    • Definition: The URL that uniquely identifies the resource or service the client wants to interact with.
    • Example: https://api.example.com/users
    • Example: https://api.example.com/profile
    • Example: https://api.example.com/items/493827
    • Example: https://api.example.com/items?name=fridge&minPrice=2000
  2. HTTP Method (Verb):

    • Definition: Type of operation the client wants to perform on the resource. Common HTTP methods include GET, POST, PUT, PATCH, and DELETE.
    • Example: GET to retrieve data, POST to create a data on the resource, PUT or PATCH to update an existing resource, and DELETE to remove a resource.
  3. Headers:

    • Definition: Additional information sent with the request, typically in the form of key-value pairs. Headers can include authentication tokens, content types, and more.
    • Example:
      Authorization: Bearer your_access_token (With this token, API can detect that client is authenticated so it performs the operations client asked.)
      Content-Type: application/json
      
  4. Parameters (Query Parameters or Path Parameters):

    • Definition: Additional data sent with the request to modify the request or filter the response. Parameters can be included in the URL (query parameters) or within the URL path (path parameters). API can internally resolve this path parameters and performs the operations based on this query parameters.
    • Example (Query Parameters): https://api.example.com/users?role=admin (**Meaning: Get the users which have admin role)
    • Example (Path Parameters): https://api.example.com/users/{id} (**Meaning: Get the user with the given id)
  5. Request Body:

    • Definition: Contains data that the client wants to send to the server, typically used in POST, PUT, and PATCH requests. The format of the data (e.g., JSON or XML) is specified in the Content-Type header.
    • Example: POST Request that creates a new user:
      {
        "username": "john_doe",
        "email": "[email protected]",
        "password": "secretpassword"
      }
      
  6. Authentication:

    • Definition: Information used to authenticate the client's identity. Common methods include API keys, OAuth tokens, or basic authentication.
    • Example: Using an API key:
      Authorization: ApiKey your_api_key
      

These components define the main structure and purpose of the REST API requests. The server processes the requests based on these given components and returns an appropriate response.

Examples of a REST API Usage (JSONPlaceholder)

Here are some examples of using REST API, using the JSONPlaceholder API , which is an extremely basic mock API built for testing and learning purposes. Each endpoints below will send GET requests to the API. Click to see API responses for requests.

Get all posts

Get post with postId:4

Get all comments

Get the comments belonging the post with id 1

Wikidata API

Wikibase Rest API

  • Wikidata is a free and collaborative knowledge base that can be edited by anyone, serving as a central repository for structured data.
  • The Wikidata API allows developers to access and interact with the data stored in Wikidata programmatically.

Documentation

  • The official documentation for the Wikidata API can be found on the Wikimedia website.

Endpoint

  • The Wikibase Rest API endpoint is https://wikidata.org/w/rest.php/wikibase/v0.

Data Retrieval

  • The API provides methods to retrieve information about items, properties, queries, and more.
  • One can retrieve data in various formats such as JSON, XML, and others.
  • SPARQL (SPARQL Protocol and RDF Query Language) is commonly used to query Wikidata. SPARQL queries can be executed using the API.

MediaWiki Action API

  • The MediaWiki Action API provides a way to access and interact with MediaWiki-powered wikis programmatically.
  • Designed to allow developers to perform various actions, retrieve data, and manipulate content on MediaWiki wikis.

Documentation

  • Official documentation for the MediaWiki Action API is available on the MediaWiki website.

Endpoint

  • The API endpoint is typically https://www.mediawiki.org/w/api.php.

Actions

  • The API supports a range of actions, including fetching page content, editing pages, searching, and more.
  • Some actions may require authentication, and OAuth is commonly used for secure access.