Prerequisites
Before you begin, ensure you have the following installed:- Node.js (v12 or higher)
- npm (comes with Node.js)
- MySQL (v5.7 or higher)
- Git
Setup Instructions
Install dependencies
Install all required Node.js packages using npm:This will install the following key dependencies:
- express (4.17.1)
- jsonwebtoken (8.5.1)
- bcrypt (5.0.1)
- mysql (2.18.1)
- @woocommerce/woocommerce-rest-api (1.0.1)
- And other supporting packages
Configure environment settings
Create your configuration file at Configuration Parameters:
config/default.json with your database credentials and JWT secret:config/default.json
app.port- Port number for the server (default: 8080)app.JWT_SECRET- Secret key for JWT token signing and verificationdb.host- MySQL database host addressdb.db- Database namedb.user- Database usernamedb.password- Database password
Set up MySQL database
Create the required MySQL database and tables. Connect to your MySQL server:Create the database:Key Tables:
The API requires the following tables:
usuarios, productossae, productos_publicados, and bitacora. Refer to your database schema documentation for the complete table structure.usuarios- User accounts with encrypted passwordsproductossae- Product catalog with pricing and inventoryproductos_publicados- Tracks products published to WooCommercebitacora- Audit log for user actions
Start the server
You can start the server in two modes:Development mode uses nodemon for automatic server restarts on file changes.You should see the server start successfully. The API will be available at:The server is configured in
server.js:31 to listen on the port specified in your config or environment variable PORT.Test the API
Verify the API is running by making a request to the health check endpoint:Expected response:This confirms your API is up and running successfully!
Authenticate and get your token
To access protected endpoints, you need to authenticate. First, ensure you have a user account in the database.Make a login request:Successful authentication returns:
Save this token! You’ll need it to access all secured endpoints. The token expires after 1 hour.
Server Configuration Details
The server (server.js) is configured with the following middleware:
server.js
server.js:21-27
Common Issues
Connection to database failed
Connection to database failed
- Verify your MySQL server is running
- Check database credentials in
config/default.json - Ensure the database exists and user has proper permissions
- Test connection:
mysql -h host -u user -p
Port already in use
Port already in use
- Another process is using port 8080
- Change the port in
config/default.jsonor set the PORT environment variable:
JWT_SECRET not found
JWT_SECRET not found
- Ensure
JWT_SECRETis properly configured inconfig/default.json - The JWT secret is loaded from config on server startup (see
server.js:9)
401 Unauthorized on secured endpoints
401 Unauthorized on secured endpoints
Next Steps
Now that your API is running, you can:- Learn more about Authentication and JWT token management
- Explore the API endpoints in the API Reference section
- Set up WooCommerce integration for product publishing
- Create user accounts with different access levels
Authentication Guide
Deep dive into JWT authentication and security
API Reference
Complete API endpoint documentation