How to make a simple POST request or an API endpoint in Nestjs?
Tags: resource
Published: 2022-01-31 00:00:00

---
  title: How to make a simple POST request or an API endpoint in Nestjs?
  published: true
  tags: Resource
  date: Mon Jan 31 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/make-simple-post-request-api-endpoint-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-4dc364b91489b0cdb331854b89c23248.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/make-simple-post-request-api-endpoint-nestjs)
  
To make a simple `POST` request or an API endpoint in Nestjs, there are 3 things we need to define such as:

- a valid typescript class to write various request methods definitions, in our case we will be writing only for the `POST` request method.
- a `@Controller()` decorator function before the controller class to define the metadata for the route.
- a `@Post()` decorator function to set the metadata to define that a specific method in the `class` is used for a particular `POST` request.

### TL;DR

```ts
import { Controller, Post } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // the @Post() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `POST` to `/greet` endpoint
  @Post()
  sayHello() {
    return `Hello World`;
  }
}
```

**NOTE: To generate the basic boilerplate code with the main `app` module, see the blog on [How to create a new project or an app in Nestjs?](https://melvingeorge.me/blog/create-new-project-nestjs) if you have not made a Nestjs project.**

For example, let's say we need to make a simple `POST` request API endpoint called `/greet` and on requesting to this endpoint it should return a simple string response called `Hello World!`.

So to do this first let's make a file to write the `POST` request code, let's name it `greet.controller.ts`.

To make this file in the terminal you can use the `mkdir` command to make a directory called `greet` and then use the `touch` command to quickly make a controller file.

It can be done like this,

```bash
mkdir greet && touch greet/greet.controller.ts
```

After running the above command, you will have a structure that looks something like this,

```bash
- greet
 - greet.controller.ts
```

Now inside the `greet.controller.ts` typescript file let's make a `class` called `GreetController` and export it since we will need to import this class in the main `app` module later.

It can be done like this,

```ts
export class GreetController {}
```

After defining the controller class and exporting it, now we need to import the `@Controller()` decorator function from the `@nestjs/common` module like this,

```ts
import { Controller } from "@nestjs/common";

export class GreetController {}
```

Now let's use the `@Controller()` decorator function before the `GreetController` class and pass the string of `greet` to it. This is done because the `@Controller()` decorator function accepts a parameter to define the route metadata to be used. Since we need the route to be `/greet`, we can just pass the `greet` string to it and Nestjs will handle the routing mechanism.

It can be done like this,

```ts
import { Controller } from "@nestjs/common";

// makes the /greet route
@Controller("greet")
export class GreetController {}
```

After using the `@Controller()` decorator function to define the route, let's now make a method in the class called `sayHello`. This method will be used to return the response.

It can be done like this,

```ts
import { Controller } from "@nestjs/common";

// makes the /greet route
@Controller("greet")
export class GreetController {
  // a simple method that returns a string
  sayHello() {
    return `Hello World`;
  }
}
```

Now we need to instruct Nestjs that when the request method is a `POST` and the route is `/greet` we need to run the `sayHello` method, to do that let's use the `@Post()` decorator function from the `@nestjs/common` module and use it before the `sayHello` method without any arguments like this,

```ts
import { Controller, Post } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // the @Post() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `POST` to `/greet` endpoint
  @Post()
  sayHello() {
    return `Hello World`;
  }
}
```

**NOTE: If you look at the `@Post()` decorator function you can see that we are not passing any arguments to the function, this is because we are not defining any nested routes for the `/greet` endpoint. For example, if you need this method to be invoked for the `/greet/hai` endpoint, you need to pass a string of `hai` to the `@Post()` decorator function.**

The code for the `/greet` controller is done. The only thing left is to import our `GreetController` controller class and then initialize it as a controller in the main `app` module.

If you navigate to the `app.module.ts` (This is the default filename used if you have created the Nestjs project using the [Nestjs CLI](https://melvingeorge.me/blog/create-new-project-nestjs)), there you need to import the `GreetController` class and add it as a controller in the `controllers` array in the `@Module()` decorator function.

It can be done like this,

```ts
/* app.module.ts file */
import { Module } from "@nestjs/common";

// Greet controller
import { GreetController } from "./greet/greet.controller";

@Module({
  imports: [],
  controllers: [GreetController], // <- add controller here
  providers: [],
})
export class AppModule {}
```

Now we have made a simple `POST` method `/greet` endpoint successfully in the Nestjs project. Yay 🥳!

If you make a `POST` request to the `https://localhost:3000/greet`, you can see the response of `Hello World` from the server.

See the above code live in [codesandbox](https://codesandbox.io/s/make-simple-post-request-api-endpoint-nestjs-kmbnrw?file=/src/app.module.ts).

Go to [Hoppscotch API tester 🚀](https://hopp.sh/r/kIDO21kW3yD2) to send a `POST` request to the URL in the above codesandbox container.

**NOTE: If you were not able to send a request using the above Hoppscotch URL try turning on the `proxy` in the app.**

That's all 😃!

### Feel free to share if you found this useful 😃.

---