Understanding Public API Documentation: Using https://mock.shop/

If you're a software developer, you most likely have come across the term "API" or "Application Programming Interface" before. An API essentially allows two software applications to communicate with each other, using predefined rules and protocols. One of the best ways to learn how to use an API is by reading its documentation. In this blog post, we'll explore the public API documentation for https://mock.shop/, a mock e-commerce website that allows developers to practice and test their API integration skills.

Getting Started with https://mock.shop/

First, you'll need to sign up for a free account on https://mock.shop/ to access the API. Once you've done that, you can find the API documentation at https://mock.shop/docs.

The documentation clearly outlines the available endpoints, their corresponding request methods, and the expected response format. It also includes example request and response data, as well as code snippets to help you get started quickly.

Using https://mock.shop/ API with JavaScript

Here are a few example code snippets in JavaScript that demonstrate how to use https://mock.shop/ API:

Example 1: Retrieving a List of Products

To fetch the list of available products, you can send a GET request to the /products endpoint. Here's how you can do it using fetch API in JavaScript:

fetch('https://mock.shop/api/v1/products')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

This will log the list of products to the console.

Example 2: Retrieving Details of a Specific Product

To fetch the details of a specific product, you can send a GET request to the /products/{productId} endpoint. Here's how you can do it using axios in JavaScript:

axios.get('https://mock.shop/api/v1/products/123')
  .then(response => console.log(response.data))
  .catch(error => console.error(error));

This will log the details of the product with ID 123 to the console.

Example 3: Creating a New Order

To create a new order, you can send a POST request to the /orders endpoint with the relevant order data. Here's an example using fetch API:

const data = {
  customerName: 'John Doe',
  email: 'john.doe@example.com',
  phoneNumber: '1234567890',
  items: [
    { productId: 1, quantity: 2 },
    { productId: 2, quantity: 1 }
  ]
};

fetch('https://mock.shop/api/v1/orders', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

This will create a new order with the specified data and log the response to the console.

Conclusion

API documentation is an essential resource for any developer who wants to integrate with a third-party API. The https://mock.shop/ API documentation is comprehensive and user-friendly, making it easy to get started with integrating the API into your application. By following the examples provided in this blog post, you should be able to start experimenting with the API and building your own integrations.

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf