API - wbobeirne/nycda-ecommerce-server GitHub Wiki

Table of Contents


Accessing the API

The API is available at [domain]/api, like localhost:7000/api or http://myapp.heroku.com/api. You should create an API client for accessing the API. The API only accepts JSON bodies for POSTing, so don't try to send form data to it. Likewise, GET requests only accept query parameters.

All of the endpoints below should be prefixed with the URL. So /products would be accessed at localhost:7000/api/products or http://myapp.heroku.com/api/products.


Responses

Responses from the API only come in two flavors, successful ones, and errorous ones. The response is always a JSON object that has the same shape, depending which of the two it is.

Success Responses

Any 2XX response means everything went OK. It'll always have this shape, a lone data key:

{
  data: {
    /* ... */
  },
}

Data could have any number of keys in it, but it'll always be an object.

Error Responses

Any 4XX or 5XX response means something went wrong. It'll always have this shape, a lone error key:

{
  error: {
    code: 500, // The code of the response, 4XX or 5XX
    type: "SOME_ERROR", // A developer-readable error "type"
    message: "Something went wrong!", // A user-readable error message
    data: { /* ... */ }, // OPTIONAL: Some data about what went wrong
  }
}

Images

Any of the images listed below have the following shape:

{
  small: "http://url.to/image-small.jpg",
  medium: "http://url.to/image-small.jpg",
  large: "http://url.to/image-small.jpg",
  original: "http://originalurl.com/whateverisintheform.jpg"
}
  • small - Max dimension of 160px
  • medium - Max dimension of 400px
  • large - Max dimension of 800px
  • original - URL to the original source image, avoid using if possible

Endpoints

GET /products

Gets all products, with optional sort and search parameters. Only gets a subset of the product, so you'll need to fetch the individual product to display all of it.

Params

Name Type Description
sort string OPTIONAL. How to sort the products, acceptable inputs are priceHigh, priceLow, AtoZ, ZtoA, ratingHigh, and ratingLow. If an invalid sort is sent, it'll default to last edited.
search string OPTIONAL. A keyword to search by. Searches names AND description.
category string OPTIONAL. Only return products of a certain category.

Response

{
  data: {
    products: [{
      name: "Name of Product",
      price: "29.98",
      category: "category", // Not always here!
      rating: 9, // Out of 10, not always here!
      image: { /* Typical image... */ } // Guaranteed to be here
    }, /* ... */],
  }
}

Possible Errors

  • DB_ERROR (500) - Only if something really bad happens to the DB


GET /products/:productId

Fetch a particular product. Comes with all of the products information.

Response

{
  data: {
    product: {
      name: "Name of Product",
      price: "29.98",
      category: "category", // Not always here!
      rating: 7, // Out of 10, not always here!
      description: "The full length description of the product", // Not always here!
      images: [/* Array of image(s)... */],
      specs: [{
        label: "Label",
        value: "Value"
      }, /* ... */] // Guaranteed array, but might be empty!
    }
  }
}

Possible Errors

  • BAD_PARAM (400) - If an invalid product ID is sent (like a product doesn't exist)
  • DB_ERROR (500) - Only if something really bad happens to the DB


POST /orders

Create a new order given a set of products, and some information about who to ship it to. Creates a new order and returns just the ID to you. You can later fetch the whole order

Params

Name Type Description
products array[productId] Array of product IDs that you're buying
name string Shipping name
address string Shipping street address
city string Shipping city
state string State key, ala "az", not "Arizona"
zipcode string 5 number zipcode only, no more, no less
address2 string OPTIONAL. Secondary address

Response

{
  data: {
    orderId: "835137951"
  }
}

Possible Errors

  • BAD_PARAM (400) - If any invalid fields are sent, data will be an array of { field: "address", error: "Address is required" } if you want to show detailed errors
  • DB_ERROR (500) - Only if something really bad happens to the DB


GET /orders/:orderId

Fetch a created order.

Response

{
  data: {
    order: {
      products: [/* List of products, identical to GET `/products` */],
      name: "John Smith",
      address: "1122 Lane Ave",
      address2: "Apt 209", // Not always there
      city: "New York",
      state: "NY",
      zipcode: "10001",
      createdAt: "2017-08-12 18:32:50.005",
    }
  }
}

Possible Errors

  • BAD_PARAM (400) - If an invalid order ID is sent (like an order doesn't exist)
  • DB_ERROR (500) - Only if something really bad happens to the DB
⚠️ **GitHub.com Fallback** ⚠️