# Clerk Express SDK

The Clerk Express SDK provides a powerful set of tools and utilities to seamlessly integrate authentication, user management, and Organization management into your Express application. Refer to the [quickstart](https://clerk.com/docs/expressjs/getting-started/quickstart.md) to get started.

> If you are upgrading from the Node SDK, see the [upgrade guide](https://clerk.com/docs/guides/development/upgrading/upgrade-guides/node-to-express.md) for more information.

## `clerkMiddleware()`

The `clerkMiddleware()` middleware checks the request's cookies and headers for a session JWT and if found, attaches the [`Auth`](https://clerk.com/docs/reference/backend/types/auth-object.md) object to the `request` object under the `auth` key. See the [reference doc](https://clerk.com/docs/reference/express/clerk-middleware.md) for more information.

## `requireAuth()`

The `requireAuth()` middleware acts similarly to [`clerkMiddleware()`](#clerk-middleware), but also protects your routes by redirecting unauthenticated users to the homepage. See the [reference doc](https://clerk.com/docs/reference/express/require-auth.md) for more information.

## `getAuth()`

The `getAuth()` helper retrieves authentication state from the `request` object. It returns the [`Auth` object](https://clerk.com/docs/reference/backend/types/auth-object.md){{ target: '_blank' }}, which includes helpful authentication information like the user's ID, session ID, and Organization ID. It's also useful for protecting routes. See the [reference doc](https://clerk.com/docs/reference/express/get-auth.md) for more information.

## `clerkClient`

[`clerkClient`](https://clerk.com/docs/reference/backend/overview.md) is a wrapper around the [Backend API](https://clerk.com/docs/reference/backend-api){{ target: '_blank' }} that makes it easier to interact with the API. For example, to retrieve a list of all users in your application, you can use the `users.getUserList()` method instead of manually making a fetch request to the `https://api.clerk.com/v1/users` endpoint.

To access a resource, you must first instantiate a `clerkClient` instance. See the [reference documentation](https://clerk.com/docs/reference/backend/overview.md) for more information.

### Example: Use `clerkClient` to get a user's information

The following example uses `clerkClient` to get information about the currently signed-in user. If the user is authenticated, their `userId` is passed to [`clerkClient.users.getUser()`](https://clerk.com/docs/reference/backend/user/get-user.md){{ target: '_blank' }} to get the current user's [User](https://clerk.com/docs/reference/objects/user.md){{ target: '_blank' }} object. If not authenticated, the request is rejected with a `401` status code.

```js
import { clerkClient, clerkMiddleware, getAuth } from '@clerk/express'
import express from 'express'

const app = express()
const PORT = 3000

// Apply `clerkMiddleware()` to all routes
app.use(clerkMiddleware())

app.get('/user', async (req, res) => {
  // Use `getAuth()` to access `isAuthenticated` and the user's ID
  const { isAuthenticated, userId } = getAuth(req)

  // If user isn't authenticated, return a 401 error
  if (!isAuthenticated) {
    return res.status(401).json({ error: 'User not authenticated' })
  }

  // Use `clerkClient` to access the `getUser()` method
  // to get the user's Backend `User` object
  const user = await clerkClient.users.getUser(userId)

  res.json(user)
})

// Start the server and listen on the specified port
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`)
})
```

---

## Sitemap

[Overview of all docs pages](https://clerk.com/docs/llms.txt)
