Skip to content

eludadev/bikes-api

Repository files navigation

Bikes API, with Cyclic 🚲️

Deploy on Cyclic 🚀

Deploy to Cyclic

Fill Database with Mock Data

npm run fill-db # fills database with 25 mock bike items

Fetching Bikes

GET routes

List all bikes

curl http://localhost:3000/bikes/all | jq .

Get bike by ID

curl http://localhost:3000/bikes/<ID> | jq . # replace <ID> with an ID from the response to /all

Get bike by Handle

curl http://localhost:3000/bikes/by-handle/<HANDLE> | jq . # replace <HANDLE> with a handle from the response to /all

Search bikes by Title

curl http://localhost:3000/bikes/search/by-title\?query\=Bicycle | jq .

Creating and Replacing Bikes (Authentication required!)

POST & PUT routes

Create new bike

// request.json (remove this line from the actual file!)
{
  "productType": "Hybrid Bicycle",
  "createdAt": "Fri Jan 28 2022 03:13:24 GMT+0100 (GMT+02:00)",
  "vendor": "Audi",
  "totalInventory": 2,
  "availableForSale": false,
  "priceRange": {
    "minPrice": {
      "currencyCode": "USD",
      "amount": 1160
    },
    "maxPrice": {
      "currencyCode": "USD",
      "amount": 1624
    }
  },
  "description": "Autem ipsam quasi omnis ut. Et officiis quia. Sed quaerat pariatur nihil nobis est quos earum quidem.",
  "title": "Hybrid Bicycle Sentra"
}
export TOKEN=<TOKEN> # replace <TOKEN> with the token from /api/user
curl -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" http://localhost:3000/bikes/ -d @request.json | jq .

Replace existing bike

// request.json (remove this line from the actual file!)
{
  "productType": "Hybrid Bicycle",
  "createdAt": "Fri Jan 28 2022 03:13:24 GMT+0100 (GMT+02:00)",
  "vendor": "Audi",
  "totalInventory": 2,
  "availableForSale": false,
  "priceRange": {
    "minPrice": {
      "currencyCode": "USD",
      "amount": 1160
    },
    "maxPrice": {
      "currencyCode": "USD",
      "amount": 1624
    }
  },
  "description": "Autem ipsam quasi omnis ut. Et officiis quia. Sed quaerat pariatur nihil nobis est quos earum quidem.",
  "title": "Hybrid Bicycle Sentra",
  "id": "4139c5ae-b83d-4a0c-9b75-12d182aaed7c",
  "handle": "hybrid-bicycle-sentra"
}
export TOKEN=<TOKEN> # replace <TOKEN> with the token from /api/user
curl -X PUT -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" http://localhost:3000/bikes/<ID> -d @request.json | jq . # replace <ID> with an ID from the response to /all

Updating and Deleting (Authentication required!)

PATCH & DELETE routes

Update parts of existing bike

// request.json (remove this line from the actual file!)
{
  "totalInventory": 4,
  "availableForSale": true
}
export TOKEN=<TOKEN> # replace <TOKEN> with the token from /api/user
curl -X PATCH -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" http://localhost:3000/bikes/<ID> -d @request.json | jq . # replace <ID> with an ID from the response to /all

Delete existing bike

export TOKEN=<TOKEN> # replace <TOKEN> with the token from /api/user
curl -X DELETE -H "Authorization: Bearer $TOKEN" http://localhost:3000/bikes/<ID> | jq . # replace <ID> with an ID from the response to /all

Authentication

Get bearer authentication token:

curl -H 'Content-Type: application/json' http://localhost:3000/api/user -d '{"username": "cyclic"}' | jq .token -r