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

---
  title: How to make a simple GET request or an API endpoint in Nestjs?
  published: true
  tags: Resource
  date: Thu Jan 27 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/make-simple-get-request-api-endpoint-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-3167aacfbedcac7a673e3d99d7faceb7.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/make-simple-get-request-api-endpoint-nestjs)
  
To make a simple `GET` 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 `GET` request method.
- a `@Controller()` decorator function before the controller class to define the metadata for the route.
- a `@Get()` decorator function to set the metadata to define that a specific method in the `class` is used for a particular `GET` request.

### TL;DR

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

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetControler {
  // the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  @Get()
  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 `GET` request API endpoint called `/greet` and on requesting to this endpoint it should return a string response called `Hello World!`.

So to do this first let's make a file to write the `GET` 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 `GreetControler` 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 GreetControler {}
```

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 GreetControler {}
```

Now let's use the `@Controller()` decorator function before the `GreetControler` 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 GreetControler {}
```

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 GreetControler {
  // a simple method that returns a string
  sayHello() {
    return `Hello World`;
  }
}
```

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

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

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

**NOTE: If you look at the `@Get()` 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 `@Get()` decorator function.**

The code for the `/greet` controller is done. The only thing left is to import our `GreetControler` 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 `GreetControler` 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 { GreetControler } from "./greet/greet.controller";

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

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

If you navigate to the `https://localhost:3000/greet` from your browser, you can see the response of `Hello World` from the server.

See the above code live in [codesandbox](https://codesandbox.io/s/make-simple-get-api-endpoint-nestjs-u2sd1r?file=/src/greet/greet.controller.ts).

That's all 😃!

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

---