How to explore our API using Insomnia or Postman?
Exploring and testing an API can be a daunting task, but with the right tools, such as Insomnia or Postman, it is much easier than it seems. Insomnia is a software that allows us to make requests in an organized and structured way to our API. You can also use Postman, and both will offer you an effective way to interact with the API. In the project repository, you will find files like insomnia.json
or postman.json
to import the whole API and quickly configure it in the tool of your choice.
How to configure a request environment?
-
Import configuration file:
- For Insomnia: Import the
insomnia.json
file.
- For Postman: Use the
postman.json
file.
-
Configure local environment:
- Verify that the application is running on
localhost
port 3000
.
- Make a request to verify that you can receive the "HelloMyServer on express" response.
-
Subdirectories and endpoints:
- The API is structured in subdirectories such as categories, notification, customers, orders, products, profile and users.
- Each subdirectory contains specific endpoints for your services, such as getting all categories, creating new ones, etc.
How to create a product in the API?
Before creating a product, it is vital that the database and migrations are configured correctly, as the API makes use of a migration system that structures the necessary tables.
-
Raise the database:
-
Run the migrations:
- Run the command for migrations:
npm run migrations:run
- Restart the development server:
npm run dev
-
Create a category:
- Make a request to create a category, providing the
name
and image
.
How to authenticate and create data?
The API has authentication protections, which means that some actions require you to be logged in as an administrator.
-
Create an administrator user:
- Access the endpoint
users/create
and create a user with email and password.
-
Login:
- Login to the
auth/login
endpoint using the email and password to obtain an access token.
-
Use access token:
- Specify the access token in the environment variables in Insomnia or Postman.
- Insert it in the authentication section as Bearer Token.
How to handle protected endpoints?
There are endpoints in the API that are protected, such as creating categories, which requires authentication. On the other hand, there are some, such as creating products, that do not require verification. It is relevant to distinguish these cases and prepare an appropriate testing scheme.
-
Test protections:
- Try to create a category without authentication and receive the 401 error response indicating that it is an unauthorized action.
-
Configure and validate users:
- Create a user and make sure they are registered in the database.
- Use their authentication to perform administrative actions.
How to practice and get familiar with the API?
Invest time in interacting with the API by creating categories and products. For example:
- Create at least three categories with your admin user.
- Develop ten products distributed among those categories.
These activities will provide you with relevant data in the database to perform experiments and tests with the API. This kind of practice will help you learn more about the inner workings and structuring of the API.
Don't stop here! Keep exploring and expand your skills with the tool you feel most comfortable with, and you will soon see the fruits of your efforts in professional development.
Want to see more contributions, questions and answers from the community?