Nest JS for Beginners #1: Create Your First API
Tags: tutorial, nestjs, api, httpget
Published: 2023-12-02 08:13:33

#Prerequisites:

1 JavaScript

2 TypeScript (Optional)

3 Basics of Object-Oriented Programming (OOP)

4 Postman (for API Testing)

4 Code Editor IDE

5 Node.js

# What is Nest.js?
Nest.js is a framework designed for building highly scalable server-side applications using Node.js. Notably, it provides full support for TypeScript, enabling developers to write clean and reusable code.

Nest.js seamlessly combines Object-Oriented Programming (OOP) and Functional Programming principles. For those familiar with server-side development, Nest.js may evoke comparisons with Express.js and Fastify libraries. Under the hood, Nest.js uses Express.js as its default HTTP server, but it offers easy integration with Fastify as well.


# Generating a Nest.js Project:
Before generating a Nest.js project, ensure that you have the latest version of Node.js installed on your system. Follow these steps:

1 Install the Nest CLI globally:
npm install -g @nestjs/cli

2 Generate a new Nest.js project:
nest new Hello-World

When prompted, choose a package manager (npm, yarn, pnpm).

Open the project in your preferred code editor.

# Understanding Nest.js Project Structure:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iychvmpknzws4zkxv91l.jpg)


  
# Basic Understanding of API:
In simple terms, an API (Application Programming Interface) is a means by which different software components communicate. Imagine sending raw cloth material to a tailor with design instructions, and receiving stitched cloth in return. In the computer world, frontend applications request data from servers through APIs, receiving the requested data as a response.

Creating Your First API:

1 Start the server:
 npm run start:dev
Replace yarn with npm or pnpm if you're using those package managers.

2 Open Postman and test the default API:

Choose the GET method.
Add the API URL: http://localhost:3000
Hit send. You should see the response, "Hello World!"



Customize the API to get a list of user names:

3 Open app.controller.ts and modify the code.

4 Open app.service.ts and update the function accordingly.

5 Test the updated API in Postman with the URL http://localhost:3000/users.


# Customizing the API to Fetch User Data:

# Step 1: Adjust app.controller.ts

In the app.controller.ts file, implement the following updates:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9g813mlb2b48g2w04df.png)



Explanation:

Defined the /users route within the @Controller() decorator, establishing the endpoint for obtaining user data.

Renamed the function to getUsers to signify its purpose of fetching user information.

Modified the return type to an array of strings (string[]).

Synchronized the function call with the updated service method name.

# Step 2: Refine app.service.ts

Navigate to the app.service.ts file and refine it as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb2dwyaylbc5gq9ugqun.png)



 Explanation:

Renamed the function to fetchUserNames for clarity in the service layer.
Modified the return type to an array of strings (string[]).
Provided an updated array containing user names.

# Testing the Enhanced User API:

1 Open Postman.

2 Choose the GET method.

3 Enter the API URL: http://localhost:3000/users

4 Execute the request.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7tsakylw3acdtzm63te.png)



# Outcome:
If the response includes a list of user names like 'Alice Johnson' and 'Bob Smith,' congratulations! 🎉 You've successfully tailored and personalized your initial API in Nest.js to fetch user data.