This project is a REST API built with Node.js and Express that converts natural language queries into SQL statements using Google's Gemini AI and executes them on a MySQL database.
- Convert natural language queries into SQL statements
- Execute generated SQL queries on a MySQL database
- Return the results as a JSON response
- Node.js
- Express.js
- MySQL (mysql2 package)
- Google's Gemini AI API
- Axios
- dotenv (for environment variables management)
- Node.js and npm installed
- MySQL database configured
- Google Gemini AI API key
- Clone the repository:
git clone <repository-url> cd <project-directory>
- Install dependencies:
npm install
- Create a
.env
file and configure the following environment variables:DB_HOST=<your-mysql-host> DB_USER=<your-mysql-username> DB_PASSWORD=<your-mysql-password> DB_NAME=<your-database-name> GEMINI_API_KEY=<your-gemini-api-key>
Start the server:
node index.js
The server will run on port 5000
by default.
Endpoint: POST /query
Request Body:
{
"query": "Show all users who registered last week"
}
Response:
{
"sql": "SELECT * FROM users WHERE registration_date >= NOW() - INTERVAL 7 DAY;",
"data": [
{
"id": 1,
"name": "John Doe",
"registration_date": "2024-02-20"
}
]
}
If the API encounters an error, it returns a response with an error message:
{
"error": "Internal Server Error"
}
This project is open-source and available under the MIT License.