---
title: "Macro"
description: "Macro is a reusable route option. Learn how to create and use macros in Elysia to enhance your application."
url: "https://next.elysiajs.com/tutorial/patterns/macro/"
---

# Macro

Reusable route options.

Imagine we have an authentication check like this:

```typescript
import { Elysia, t } from 'elysia'

new Elysia()
  .post('/user', {
    cookie: t.Object({
      session: t.String()
    }),
    beforeHandle({ cookie: { session } }) {
      if(!session.value) throw 'Unauthorized'
    }
  }, ({ body }) => body)
  .listen(3000)
```

If we have multiple routes that require authentication, we have to repeat the same options over and over again.

Instead, we can use a macro to reuse route options:

```typescript
import { Elysia, t } from 'elysia'

new Elysia()
  .macro({ auth: {
    cookie: t.Object({
      session: t.String()
    }),
    // psuedo auth check
    beforeHandle({ cookie: { session }, status }) {
      if(!session.value) return status(401)
    }
  } })
  .post('/user', {
    auth: true
  }, ({ body }) => body)
  .listen(3000)
```

**auth** will then inline both **cookie**, and **beforeHandle** to the route.

Simply put, [Macro](/patterns/macro/) **is a reusable route option**, similar to function but as a route option with **type soundness**.

## Assignment

Let’s define a macro to check if a body is a Fibonacci number:

```typescript
function isFibonacci(n: number) {
  let a = 0, b = 1
  while(b < n) [a, b] = [b, a + b]
  return b === n || n === 0
}
```
