Welcome to the FITS-AI API user guide. This document explains how to set up and use the FITS-AI API, assuming you have access to the provided .env example configuration file.
Before starting, ensure the following tools are installed on your system:
- Node.js (v14 or later)
- npm or yarn for package management
- A MySQL database server running and accessible
- Postman or another API testing tool (optional but recommended)
-
Clone the FITS-AI Repository
Clone the FITS-AI API source code from your repository:git clone <repository-url> cd <repository-directory>
-
Install Dependencies
Install the required Node.js dependencies:npm install # or yarn install -
Create and Configure the
.envFile
The.envfile is used to configure environment variables. Use the.env.exampleas a template:cp .env.example .env
Open the
.envfile and configure the variables:DATABASE_URL="mysql://user@host:port/db_name" JWT_SECRET="your_jwt_secret" REFRESH_TOKEN_SECRET="your_refresh_token_secret"
- Replace
userwith your MySQL username. - Replace
hostwith your database host (e.g.,localhost). - Replace
portwith your database port (default is3306). - Replace
db_namewith the name of your MySQL database. - Replace
your_jwt_secretandyour_refresh_token_secretwith secure random strings for token generation.
- Replace
-
Migrate the Database
Ensure the database is set up correctly by running migrations:npm run migrate # or yarn migrate
-
Start the development server:
npm run start-dev # or yarn start-dev -
The API should now be running on
http://localhost:3000(default). -
To start the production server:
npm start # or yarn start
-
Generate an Access Token
Use the/auth/loginendpoint to authenticate a user and generate an access token. Example payload:{ "email": "example_email", "password": "example_password" }The response will include an access token and a refresh token.
-
Use the Access Token
Include the access token in theAuthorizationheader of your API requests:Authorization: Bearer <access_token> -
Refresh the Token
Use the/auth/refreshendpoint to refresh the access token when it expires. Include the refresh token in the payload.
Endpoint: /api/v1/products
Method: GET
Headers:
Authorization: Bearer <access_token>Response:
{
"success": true,
"message": "Products retrieved successfully.",
"userProducts": [
{
"id": "GZsGCpYuZHXntJo7o6THZ",
"product_id": "3D4bx3VNNKVy1t6Y3s_dw",
"created_at": "2024-11-18T11:06:01.000Z",
"users_id": "B0yx3KGXStC1j683cSr_b",
"product": {
"id": "3D4bx3VNNKVy1t6Y3s_dw",
"name": "Coca-cola",
"grade": {
"id": 1,
"grade_name": "A",
"grade_desc": "Product ini bagus"
},
"calories": "200 kcal",
"calories_ing": "sugar, flour",
"protein": "10 g",
"protein_ing": "soy, milk",
"fat": "5 g",
"fat_ing": "butter, cream",
"fiber": "2 g",
"fiber_ing": "oats, flaxseed",
"carbo": "30 g",
"carbo_ing": "wheat, rice",
"sugar": "15 g",
"sugar_ing": "sucrose, honey"
}
}
]
}Endpoint: /api/v1/users/allergy
Method: POST
Headers:
Authorization: Bearer <access_token>Payload:
{
"data": [
{
"allergy_name": "Lactose Intolerance"
}
]
}Response:
{
"success": true,
"massage": "User allergy added"
}-
Database Connection Failed
Ensure theDATABASE_URLin the.envfile is correctly configured and the MySQL server is running. -
JWT Token Errors
- Ensure
JWT_SECRETandREFRESH_TOKEN_SECRETare set correctly in the.envfile. - Verify the tokens are included in the API request headers.
- Ensure
-
Server Not Starting
Check for missing dependencies or configuration issues in the.envfile. Run:npm run check-env
If you encounter issues not covered here, please contact support at bimagung2203@gmail.com.