> For the complete documentation index, see [llms.txt](https://docs.adminjs.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.adminjs.co/installation/plugins/koa.md).

# Koa

{% hint style="info" %}
Make sure you have installed AdminJS packages described in [Getting started](/installation/getting-started.md) article.

```bash
$ yarn add adminjs @adminjs/koa
```

{% endhint %}

To setup AdminJS panel with Koa you need to have `koa` installed and required peer dependencies:

```bash
$ yarn add koa @koa/router koa2-formidable
```

Afterwards, follow one of the examples below.

### Simple

{% tabs %}
{% tab title="Javascript" %}
{% code title="app.js" %}

```javascript
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'

const PORT = 3000

const start = async () => {
  const app = new Koa()
  const admin = new AdminJS({
    resources: [],
    rootPath: '/admin',
  })

  const router = AdminJSKoa.buildRouter(admin, app)

  app
    .use(router.routes())
    .use(router.allowedMethods())

   app.listen(PORT, () => {
     console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
   })
}

start()
```

{% endcode %}
{% endtab %}

{% tab title="Typescript" %}
Install additional dependencies:

```bash
$ yarn add -D @types/koa
```

{% code title="app.ts" %}

```typescript
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'

const PORT = 3000

const start = async () => {
  const app = new Koa()
  const admin = new AdminJS({
    resources: [],
    rootPath: '/admin',
  })

  const router = AdminJSKoa.buildRouter(admin, app)

  app
    .use(router.routes())
    .use(router.allowedMethods())

   app.listen(PORT, () => {
     console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
   })
}

start()
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Authenticated

To add authentication, you must use `AdminJSKoa.buildAuthenticatedRouter` instead of `AdminJSKoa.buildRouter`.

{% tabs %}
{% tab title="Javascript" %}
{% code title="app.js" %}

```javascript
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'

const PORT = 3000

const DEFAULT_ADMIN = {
  email: 'admin@example.com',
  password: 'password',
}

const authenticate = async (email, password) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN)
  }
  return null
}

const start = async () => {
  const app = new Koa()

  const admin = new AdminJS({
    resources: [],
    rootPath: '/admin',
  })

  app.keys = ['your secret for koa cookie'];
  const router = AdminJSKoa.buildAuthenticatedRouter(
    admin,
    app,
    {
      authenticate,
      sessionOptions: {
        // You may configure your Koa session here
        httpOnly: process.env.NODE_ENV === 'production',
        secure: process.env.NODE_ENV === 'production',
        renew: true,
      },
    },
  )

  app
    .use(router.routes())
    .use(router.allowedMethods())

   app.listen(PORT, () => {
     console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
   })
}

start()
```

{% endcode %}

As you may have noticed, the `authenticate` function compares credentials you submit in the form with a hardcoded `DEFAULT_ADMIN` object. In your case, you might want to modify `authenticate` function's logic to compare form credentials against real database objects.
{% endtab %}

{% tab title="Typescript" %}
Install additional dependencies:

```bash
$ yarn add -D @types/koa
```

{% code title="app.ts" %}

```typescript
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'

const PORT = 3000

const DEFAULT_ADMIN = {
  email: 'admin@example.com',
  password: 'password',
}

const authenticate = async (email: string, password: string) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN)
  }
  return null
}

const start = async () => {
  const app = new Koa()

  const admin = new AdminJS({
    resources: [],
    rootPath: '/admin',
  })

  app.keys = ['your secret for koa cookie'];
  const router = AdminJSKoa.buildAuthenticatedRouter(
    admin,
    app,
    {
      authenticate,
      sessionOptions: {
        // You may configure your Koa session here
        httpOnly: process.env.NODE_ENV === 'production',
        secure: process.env.NODE_ENV === 'production',
        renew: true,
      },
    },
  )

  app
    .use(router.routes())
    .use(router.allowedMethods())

   app.listen(PORT, () => {
     console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
   })
}

start()
```

{% endcode %}

As you may have noticed, the `authenticate` function compares credentials you submit in the form with a hardcoded `DEFAULT_ADMIN` object. In your case, you might want to modify `authenticate` function's logic to compare form credentials against real database objects.
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.adminjs.co/installation/plugins/koa.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
