# Build an API with Postman, Node.js, and MySQL

![](https://blog.postman.com/wp-content/uploads/2023/05/headshotnew-150x150.jpg)*This guest post was written by Greg Bulmash, author of [Hell on $5 A Day](http://bit.ly/hell5book "http://bit.ly/hell5book"), blogger at [LetMyPeopleCode](https://letmypeoplecode.com/greg_talks_tech "https://letmypeoplecode.com/greg_talks_tech"), and content creator for some of the biggest names in tech.*

 If you’re looking to build an API that’s highly performant, scalable, and flexible, using technologies that have strong developer communities, Node.js and MySQL make a great pair. Node.js’s asynchronous, event-driven architecture can handle a large number of concurrent requests, so scaling your API is no problem. MySQL is highly scalable as well, and its large community of users and extensive documentation make it a strong choice for a relational database. In this [Postman Quickstarts tutorial](https://quickstarts.postman.com/guide/api-builder-node-and-mysql/index.html?index=..%2F..index#0), we’ll walk you through creating a ToDo API with an OpenAPI 3 definition using Postman. You’ll learn how to generate code for your API, add business logic, and finally, generate a collection from the API definition so that you can send requests to the API. **Related: [How to create a REST API with Node.js and Express](https://blog.postman.com/how-to-create-a-rest-api-with-node-js-and-express/)**## Before you begin

 For this tutorial, you should have some familiarity with [Postman Collections](https://learning.postman.com/docs/collections/collections-overview/), and you should have a [workspace](https://blog.postman.com/solving-problems-together-with-postman-workspaces/) ready to use. As long as you have at least a beginner’s understanding of Node.js and MySQL, you’re ready to get started. Make sure you have the following: - [Node.js](https://nodejs.org/en/download) installed and in your path
- [XAMPP](https://www.apachefriends.org/) installed (or another tool you prefer for installing and administering MySQL)
- [Visual Studio Code](https://code.visualstudio.com/download) (recommended)
 
 If you need more information about installing any of the items above, see the [Install the Prerequisites](https://quickstarts.postman.com/guide/api-builder-node-and-mysql/index.html?index=..%2F..index#1) step for this Postman Quickstart. ## Step 1: Create the database

 First, let’s create the MySQL database in XAMPP: 1. 1. Open XAMPP’s Control Panel app.
    2. Select the **Manage Servers** tab.
    3. Select the **MySQL Database**, followed by the **Start** button.
    4. Select the **Apache Web Server**, followed by the **Start** button. Starting the web server allows you to access phpMyAdmin, which is a browser-based GUI tool for managing your MySQL server. When both Apache and MySQL have started, you can access phpMyAdmin at <http://localhost/phpmyadmin>:![](https://blog.postman.com/wp-content/uploads/2023/05/1-1000x496.png)
    5. In phyMyAdmin, select the **SQL** tab and add the following SQL to the text box: ```
        CREATE DATABASE todo;
        CREATE TABLE todo.todos (
        	id_code varchar(36) NOT NULL UNIQUE,
        	to_do varchar(255) NOT NULL,
        	completed boolean
        );
        CREATE USER 'todo_admin'@'localhost' IDENTIFIED BY 'leelu_dallas_multipass-6';
        GRANT SELECT, INSERT, UPDATE ON todo.* TO 'todo_admin'@'localhost';
        use todo;
        INSERT INTO todos (id_code, to_do, completed) VALUES ('todo1','Get something done', TRUE);
        INSERT INTO todos (id_code, to_do, completed) VALUES ('todo2','Get another thing done', FALSE);
        ```
    6. Select **Go** to run the query. The query creates a database and adds a table to hold your records, and then it creates a user with privileges to access and update the table. It also creates some records so that we have something to query later on.
 
 Don’t panic if you see a few `Error: #1046 No database selected` warnings while the query runs—this is expected. When the query’s done, you can turn off the Apache Web Server in the XAMPP app. Only the MySQL server is needed from here. ## Step 2: Define the API

 We’ll use [OpenAPI 3](https://spec.openapis.org/oas/latest.html) to define the ToDo API. You can download the [full API definition](https://gist.github.com/LetMyPeopleCode/99abd6eea4894d149971e98113e29076) for this tutorial from GitHub. If you want more details about this definition, see [Define the API](https://quickstarts.postman.com/guide/api-builder-node-and-mysql/index.html?index=..%2F..index#3) in the Postman Quickstart for a closer look at each section. ## Step 3: Add the API to Postman

 Let’s add the API to Postman: 1. With the Postman app or web client open, go to your workspace and select **APIs**, followed by **Create an API:![](https://blog.postman.com/wp-content/uploads/2023/05/2.png)**
2. Select the pencil icon that appears when you hover over the API’s name, and name it "ToDos".
3. Select **+** to the right of the **Definition** option, then select **Author from scratch**.
4. Select or accept **OpenAPI 3.0** as the **Definition type** and **YAML** as the **Definition Format**.
5. Select **Create Definition**. This opens a code editor.
6. Copy the full [ToDo API definition](https://gist.github.com/LetMyPeopleCode/99abd6eea4894d149971e98113e29076) from GitHub and paste it into the code editor: ![](https://blog.postman.com/wp-content/uploads/2023/05/3-1000x418.png)
7. Select **Save**.
 
## Step 4: Code the API

 You might already know that Postman can generate code to scaffold your API for multiple programming languages. If you want to code the API yourself, see [Code the API](https://quickstarts.postman.com/guide/api-builder-node-and-mysql/index.html?index=..%2F..index#5) in the Postman Quickstart. Otherwise, follow these steps to have Postman generate Node.js code for you: 1. Select the top level of the ToDos API.
2. Select **&lt;/&gt;** in the context bar.
3. Under **Language and framework**, select **NodeJs - Express**. Leave **Only generate routes and interfaces** unchecked.
4. Select **Generate Code** and download the ZIP file.
 
## Step 5: Run requests in Postman

 In order to run requests in Postman, we’ll need to generate a collection that creates `GET`, `POST`, and `PUT` queries, and then start the ToDo server to connect to the MySQL database. ### Generate a Postman Collection

 First, let’s generate a collection in Postman: 1. With the ToDos API selected, select **+** under **Collections**, and then select **Generate from definition: ![](https://blog.postman.com/wp-content/uploads/2023/05/4-1000x637.png)**
2. Name your collection, and then select **Generate Collection: ![](https://blog.postman.com/wp-content/uploads/2023/05/5.png)**
 
### Run the ToDO server

 With the collection generated, it’s time to get the ToDo server up and running: 1. In the XAMPP Control Panel, make sure that your MySQL database is running.
2. Clone the [ToDo API project repository](https://github.com/LetMyPeopleCode/ToDO_API_with_Node_and_MySQL) from GitHub.
3. In a terminal window, navigate to the top level of the repo.
4. In the terminal, enter `npm install`.
5. When the installation is complete, enter `node run start`.
 
### Try a GET request

 From your generated collection in the sidebar, select the `GET` request and then select **Send:** ![](https://blog.postman.com/wp-content/uploads/2023/05/6-1000x649.png) In the parameters, you can try changing `<boolean>` to `true` to receive a different response. ### Try a POST request

 Select the `POST` request and then select the **Body** tab: ![](https://blog.postman.com/wp-content/uploads/2023/05/7-1000x632.png) Try replacing the default values with some tasks of your own. Use the following table as a reference when you create your tasks: ![](https://blog.postman.com/wp-content/uploads/2023/05/task_creation_table-1000x282.png) If you want to verify that your new tasks were added to the database, run another `GET` request. ### Try a PUT request

 Select the `PUT` request and then select the **Body** tab: ![](https://blog.postman.com/wp-content/uploads/2023/05/8-1000x696.png) The only required parameter for this request is `id_code`. Follow these steps to set it up: 1. Set `id_code` to `todo2`. This is a value we pre-populated when creating the database.
2. Set `completed` to `true`, and then clear the `task` field.
3. Select **Send**.
 
 Sending this request updates the server to show the task is complete, then queries the database for the item and returns it to confirm the database was updated. Go back and run the `GET` request again to see that the task shows it’s complete. ## Taking it further

 Congratulations! You’ve defined an API using OpenAPI 3, generated Node.js server code in Postman, created a collection to send requests to the server, and updated the MySQL database. If you want to keep going, here are some other things you can try: - Add a `DELETE` method to the API definition.
- Update the MySQL server permissions for `todo_admin` and Node.js server code to add a `DELETE` method, then regenerate your collection to test the `DELETE` method.
- Try another [Postman Quickstart](https://quickstarts.postman.com/), or [contribute your own](https://github.com/postmanlabs/pmquickstarts)!