Code Along: CRUD API Requests

Code Along: CRUD API Requests with Colors API

During this Code Along, we’ll be using Insomnia to make API requests to a REST-based API

BASE API URL:

https://css-colors-api.herokuapp.com/api/v1

API Endpoints:

Endpoint Description
GET /colors retrieve list of colors
GET /colors/:id retrieve details for one color
POST /colors create a new color
PUT /colors/:id update an existing color
DELETE /colors/:id delete an existing color
GET /colors/search?q=purple search for a color name or hexcode
GET /colors/user_generated retrieve list of colors created by users

Examples

  • Retrieve list of all colors (GET /colors)
(GET) https://css-colors-api.herokuapp.com/api/v1/colors

  • Retrieve details for one color (GET /colors/:id)

    • Fetch the color associated with id = 3
(GET) https://css-colors-api.herokuapp.com/api/v1/colors/3

  • Create a new color (POST /colors)

    • Here we need to give the server additional information, the name and hexcode of the color we want to create

    • We are sending this additional information through a “body” of the request

  (POST) https://css-colors-api.herokuapp.com/api/v1/colors
  

request body:

  {
  	"color": {
  		"name": "Kareem Gray",
  		"hexcode": "#728ca0"
  	}
  }

  • Update an existing color (PUT /colors/:id)

    • Here we need to tell the server which color we want to update and the information we want to update

    • The update info is sent via a the “request body”

    • Here’s an example of updating the “name” of the color with the id = 7 to “Sky Blue”

  (PUT) https://css-colors-api.herokuapp.com/api/v1/colors/7
  

request body:

  {
  	"color": {
  		"name": "Sky Blue",
  	}
  }

  • Delete a color (DELETE /colors/:id)

    • Delete the color associated with id = 28
(DELETE) https://css-colors-api.herokuapp.com/api/v1/colors/28

  • Search for color by name or hexcode (GET /colors/search?q={searchTerm})

    • Search for all the colors that have the text “blue” in their name
(GET) https://css-colors-api.herokuapp.com/api/v1/colors/search?q=blue

  • Get all colors created by users (GET /colors/user_generated)

    • Retrieve all the colors that have been created by users (via a POST request)
(GET) https://css-colors-api.herokuapp.com/api/v1/colors/user_generated