- This dummy API is exclusively created for SandBox HSI, November 2024
- The API provides endpoints for user authentication, retrieving shipping rates, and creating shipments with tracking numbers.
- This API is intentionally designed with certain flaws and edge cases. Some of these flaws may even cause server termination or system errors under specific conditions. Users are encouraged to identify and document these behaviors (critical bug/defect) and are welcome to try fixing them through (local) code revisions.
- User Authentication: Register and log in to receive a Bearer token.
- Shipping Rates: Retrieve shipping rates based on origin, destination, and weight.
- Create Shipment: Generate a shipment with calculated costs and tracking numbers.
- Tracking: Track shipments with unique tracking numbers based on the date and running number format.
- Node.js with Express for the backend server
- SQLite as the database
- JWT for authentication
- bcrypt for password hashing
- Node.js and npm installed on your local machine
-
Clone the Repository:
git clone https://github.com/spartacruz/shipping-api.git cd shipping-api -
Install Dependencies:
npm install
-
Set Up Environment Variables: Create a .env file in the root directory and add the following:
ACCESS_TOKEN_SECRET=your_jwt_secret
-
Run The Server: Create a .env file in the root directory and add the following:
npm run dev
The server will run on http://localhost:3000.
The database is set up automatically on the first run with tables for Users, ShippingRates, Shipments, and Config. Seed data for ShippingRates is also automatically inserted.
API Documentation: https://sandboxhsiapidocs.notion.site/
Authenticate a user and return a Bearer token.
-
Request Body:
{ "email": "[email protected]", "password": "password" } -
Response Body:
{ "status": "success", "user": { "name": "User Name", "email": "[email protected]" }, "authorization": { "token": "jwt_token", "type": "bearer" } }
Retrieve available shipping rates based on origin, destination, and weight. Requires Bearer token authentication.
-
Query Parameters:
origin(string): Origin locationdestination(string): Destination locationweight(float): Weight in kilograms (e.g.,2.5)
-
Example Request:
curl --location 'http://localhost:3000/api/v1/shipping/rates?origin=Jakarta&destination=Bandung&weight=2.5' \ --header 'Authorization: Bearer your_token_here'
-
Response Body:
{ "status": "success", "rates": [ { "id": 1, "origin": "Jakarta", "destination": "Bandung", "service": "standard", "rate": 10000, "calculatedRate": 25000, "estimated_delivery": "3-5 business days" }, { "id": 2, "origin": "Jakarta", "destination": "Bandung", "service": "express", "rate": 20000, "calculatedRate": 50000, "estimated_delivery": "1-2 business days" } ] }
Create a new shipment. Requires Bearer token authentication.
-
Request Body:
{ "origin": "Jakarta", "destination": "Bandung", "weight": 2.5, "service": "instant", "recipient": { "name": "John Doe", "phone": "0809899999", "email": "[email protected]" } } -
Response Body:
{ "status": "success", "shipmentId": 123, "trackingNumber": "AWB202311010001" }
.
├── config
│ └── db.js # Database configuration and setup
├── controllers
│ └── shippingController.js # Controller for shipping-related endpoints
├── middleware
│ └── authMiddleware.js # Middleware for authentication
├── models
│ ├── Config.js # Model for config data
│ ├── Shipment.js # Model for shipment data
│ └── ShippingRate.js # Model for shipping rate data
├── routes
│ └── shippingRoutes.js # Route definitions for shipping API
├── .env # Environment variables
├── package.json # Package configuration
└── README.md # Project documentation
- Bearer Token: Ensure to pass the Bearer token in the headers for authenticated requests.
- Date Formatting for Tracking Numbers: Tracking numbers follow the format
AWB{YYYYMMDD}{runningNumber}. - Configurable Weight Limits: The weight limits are stored in the Config table as
maxWeightandminWeight.`
This project is open source and free for everone to fork.
Yuri - November 2024