Orders

Orders — they are the basic building blocks of LetsGo API. On this page, we'll dive into the different order endpoints you can use to manage orders programmatically. We'll look at how to query, send, update, and delete orders.

The order model

The order model contains all the information about the orders including the user identifier and total.

Properties

  • Name
    _id
    Type
    ObjectId
    Description

    Unique identifier for the order.

  • Name
    user_id
    Type
    ObjectId
    Description

    Unique identifier for the user the order belongs to.

  • Name
    total
    Type
    int-32
    Description

    The total of the order placed.

  • Name
    created_at
    Type
    date
    Description

    Timestamp of when the contact was created.

  • Name
    updated_at
    Type
    date
    Description

    Timestamp of when the contact was updated.

  • Name
    __v
    Type
    int-32
    Description

    The version key of the order.


GET/orders

List all orders

ONLY ADMIN

This endpoint allows you to retrieve all orders (including orderlines). You can also limit the number of orders you want to receive.

Required attributes

  • Name
    user_id
    Type
    ObjectId
    Description

    The _id of the user, given in token from the Authorization header.

Optional attributes

  • Name
    limit
    Type
    int
    Description

    Limit the number of orders returned.

Response

[
  {
    "order": {
        "_id": "663139efb61fff46ef57e16d",
        "user_id": {
            "_id": "662c20da7f42e1de734fe684",
            "name": "Boyd Labadie",
            "email": "123@gmail.com",
            "createdAt": "2024-04-26T21:47:06.393Z"
        },
        "total": 67000,
        "createdAt": "2024-04-30T18:35:27.059Z"
    },
    "orderLines": [
        {
            "product_id": {
                "_id": "663106066a07b082680ec491",
                "name": "LEGO ARCHITECTURE Colosseum",
                "price": 32000,
                //...
            },
            "quantity": 1
        },
        //...
    ]
  },
//...
]

GET/orders/:id

Retrieve an order

ONLY ADMIN AND USER WHO ORDERED

This endpoint allows you to retrieve an order by providing the order id. Refer to the list at the top of this page to see which properties are included with order objects.

Required attributes

  • Name
    user_id
    Type
    ObjectId
    Description

    The _id of the user, given in token from the Authorization header.

Response

{
  "order": {
    "_id": "663139efb61fff46ef57e16d",
    "user_id": {
        "_id": "662c20da7f42e1de734fe684",
        "name": "Boyd Labadie",
        "email": "123@gmail.com"
    },
    "total": 67000,
    "createdAt": "2024-04-30T18:35:27.059Z",
    "updatedAt": "2024-04-30T18:35:27.059Z"
  },
  "orderlines": [
    {
        "product_id": {
            "_id": "663106066a07b082680ec491",
            "name": "LEGO ARCHITECTURE Colosseum",
            "price": 32000,
            //...
        },
        "quantity": 1
    },
    //...
  ]
}

POST/orders/checkout

Checkout an order

This endpoint allows you to checkout an order by providing product id and quantity. It will return an url given by Stripe to checkout and pay.

Required attributes

  • Name
    user_id
    Type
    ObjectId
    Description

    The _id of the user, given in token from the Authorization header.

  • Name
    product_id
    Type
    ObjectId
    Description

    The _id of the product, the user wants to checkout.

  • Name
    quantity
    Type
    int
    Description

    The quantity of the product, the user wants to checkout.

Request

POST
/orders/checkout
curl -X POST https://lets-go-phi.vercel.app \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" \
-d '[
     {
       "product_id": "662eb3f04341bfa0b2d43ea3",
       "quantity": 13
     }
   ]'

Response

{
  "url": "https://checkout.stripe.com/c/pay/cs_test_b1Isk2tdzzVO9kwJYS3HGIaRi0reSvy7dauU7shv7AqpmK5lhPslsihA7W#fidkdWxOYHwnPyd1blpxYHZxWjA0VURfZ31Pd0A1RDRvdWo0XVFOdzFwbEFNaDNGfHVySmtRajJEPE5nRnNLTEpKXTdxUTZTaWBDVDd9Q2hjXVJQZ3w8cDNSc2JWQGlKaEBINVxRZEI9VTIxNTVfb21vPWd2dycpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPydocGlxbFpscWBoJyknYGtkZ2lgVWlkZmBtamlhYHd2Jz9xd3BgeCUl"
}

DELETE/orders/:id

Delete an order

ONLY ADMIN AND USER WHO ORDERED

This endpoint allows you to delete orders. Note: This will permanently delete the message.

Required attributes

  • Name
    user_id
    Type
    ObjectId
    Description

    The _id of the user, given in token from the Authorization header.

Response

{
  "message": "Order deleted succesfully"
}

Was this page helpful?