FakeStoreAPI Documentation

repository·master·Indexed 25 days ago

https://github.com/keikaavousi/fake-store-api

A free online mock REST API providing e-commerce JSON data for products, carts, users, and authentication. Designed for prototyping and testing shopping applications, it simulates CRUD operations without persisting data to a database. Includes detailed resource schemas and route references for managing e-commerce entities.

Tokens
2K
Snippets
2
Records
12
Agent score
83%

What's inside FakeStoreAPI

  1. Overview of FakeStoreAPI

    master

    FakeStoreAPI is a free online REST API providing pseudo-real e-commerce data (Products, Carts, Users, and Auth). It is designed for teaching, testing, and prototyping shopping websites without needing to manage a backend server.

    Important Note on Data Persistence: The API simulates CRUD operations. While POST, PUT, PATCH, and DELETE requests return successful responses and simulated data (like a new ID), the data is not actually persisted in the database. It is a mock service.

  2. Sort and Limit API results

    master

    You can use query strings to limit the number of results returned or to sort them in ascending or descending order using the limit and sort parameters.

    // Returns 3 products sorted by descending order
    fetch("https://fakestoreapi.com/products?limit=3&sort=desc")
      .then((res) => res.json())
      .then((json) => console.log(json));
  3. Initialize and run the FakeStoreAPI server

    master

    The FakeStoreAPI is an Express-based server that uses MongoDB (via Mongoose) for data persistence. To run the server, ensure you have a DATABASE_URL environment variable configured. The server listens on the port specified by PORT, defaulting to 6400 if not provided.

    Key configuration details:

    • Database: Requires DATABASE_URL to connect via Mongoose.
    • Middleware: Includes cors for cross-origin requests, express.json() for parsing JSON bodies, and express.urlencoded() for URL-encoded bodies.
    • Static Files: Serves files from the /public directory.
    • View Engine: Uses ejs with templates located in the /views directory.
  4. Manage Products via REST API

    master

    You can perform CRUD operations on products using the /products endpoint. Note that changes are not permanent.

    // Get all products
    fetch("https://fakestoreapi.com/products")
      .then((res) => res.json())
      .then((json) => console.log(json));
    
    // Get a single product
    fetch("https://fakestoreapi.com/products/1")
      .then((res) => res.json())
      .then((json) => console.log(json));
    
    // Add new product (returns fake ID)
    fetch("https://fakestoreapi.com/products", {
      method: "POST",
      body: JSON.stringify({
        title: "test product",
        price: 13.5,
        description: "lorem ipsum set",
        image: "https://i.pravatar.cc",
        category: "electronic",
      }),
    })
      .then((res) => res.json())
      .then((json) => console.log(json));
    
    // Update a product (PUT)
    fetch("https://fakestoreapi.com/products/7", {
      method: "PUT",
      body: JSON.stringify({
        title: "test product",
        price: 13.5,
        description: "lorem ipsum set",
        image: "https://i.pravatar.cc",
        category: "electronic",
      }),
    })
      .then((res) => res.json())
      .then((json) => console.log(json));
    
    // Partial update a product (PATCH)
    fetch("https://fakestoreapi.com/products/8", {
      method: "PATCH",
      body: JSON.stringify({
        title: "test product",
        price: 13.5,
        description: "lorem ipsum set",
        image: "https://i.pravatar.cc",
        category: "electronic",
      }),
    })
      .then((res) => res.json())
      .then((json) => console.log(json));
    
    // Delete a product
    fetch("https://fakestoreapi.com/products/8", {
      method: "DELETE",
    });
  5. Configure FakeStoreAPI via environment variables

    master
    The FakeStoreAPI service uses a .env file for configuration. When running via Docker Compose, the following environment variables are used to configure the application and the MongoDB database. Note that DB_USERNAME and DB_PASSWORD are required for the database initialization.
  6. Reference: Product routes and schema

    master

    Product resource schema:

    {
        "id": "Number",
        "title": "String",
        "price": "Number",
        "category": "String",
        "description": "String",
        "image": "String"
    }

    Available routes:

    • GET /products - Get all products
    • GET /products/:id - Get specific product by ID
    • GET /products?limit=5 - Limit return results
    • GET /products?sort=desc - Sort products (asc|desc, default is asc)
    • GET /products/categories - Get all categories
    • GET /products/category/:categoryName - Get all products in a specific category
    • GET /products/category/:categoryName?sort=desc - Get products in category with sorting
    • POST /products - Create a product
    • PUT, PATCH /products/:id - Update a product
    • DELETE /products/:id - Delete a product
  7. Reference: Cart routes and schema

    master

    Cart resource schema:

    {
        "id": "Number",
        "userId": "Number",
        "date": "Date",
        "products": [{"productId": "Number", "quantity": "Number"}]
    }

    Available routes:

    • GET /carts - Get all carts
    • GET /carts/:id - Get specific cart by ID
    • GET /carts?startdate=YYYY-MM-DD&enddate=YYYY-MM-DD - Get carts in date range
    • GET /carts/user/:userId - Get a specific user's cart
    • GET /carts/user/:userId?startdate=YYYY-MM-DD&enddate=YYYY-MM-DD - Get user carts in date range
    • GET /carts?limit=5 - Limit return results
    • GET /carts?sort=desc - Sort carts (asc|desc, default is asc)
    • POST /carts - Create a cart
    • PUT, PATCH /carts/:id - Update a cart
    • DELETE /carts/:id - Delete a cart
  8. Reference: User routes and schema

    master

    User resource schema:

    {
        "id": "Number",
        "email": "String",
        "username": "String",
        "password": "String",
        "name": {
            "firstname": "String",
            "lastname": "String"
        },
        "address": {
            "city": "String",
            "street": "String",
            "number": "Number",
            "zipcode": "String",
            "geolocation": {
                "lat": "String",
                "long": "String"
            }
        },
        "phone": "String"
    }

    Available routes:

    • GET /users - Get all users
    • GET /users/:id - Get specific user by ID
    • GET /users?limit=5 - Limit return results
    • GET /users?sort=desc - Sort users (asc|desc, default is asc)
    • POST /users - Create a user
    • PUT, PATCH /users/:id - Update a user
    • DELETE /users/:id - Delete a user
  9. API Route structure and base paths

    master

    The FakeStoreAPI exposes several resource-based routes. When interacting with the API, use the following base paths:

    • / : Home route
    • /products : Product management (Get all, Get single, Add, Update, Delete, Sort/Limit)
    • /carts : Cart operations
    • /users : User management
    • /auth : Authentication services