Learn how to integrate Nile Auth with your Express.js application. The integration allows the application to interact with Nile’s APIs and databases, providing tenant-aware data management. This guide provides an overview of how to use Nile-Auth core functionality with an Express.js application. We’ll cover the following topics:Documentation Index
Fetch the complete documentation index at: https://thenile.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
- Authentication, cookies, sessions
- Tenant isolation
- Securing endpoints
Authentication
Obtain Database Credentials
- If you haven’t signed up for Nile yet, sign up here and follow the steps to create a database.
- Navigate to Database Settings in your database’s UI at console.thenile.dev.
- Go to Connection settings.
- Select the CLI icon, and click Generate credentials

- Copy the required credentials and store them in an
.envfile so they can be used in the application to connect to the Nile auth service..env
Tenant Isolation and Secure Endpoints
Since Nile-Auth is designed to work with B2B applications, it is important to understand how to work with tenants, their access to data, and how to secure endpoints. We are going to extend the previous example with new functionality. We’ll add a new table to the database, and a new endpoint that queries the data, making sure the endpoint is both secure and isolated to the tenant.Create a Tenant
You do not need a new endpoint in order to extand your application with tenant functionality. Nile’s SDK includes generated routes for managing tenants.
We just need to call them:
Extract the tenant ID from the request params
There are multiple ways to pass the current tenant to the web app on each request.
You can pass it as a param, a header, or a cookie. In this example we’ll pass it as a param.Add the following code to your
server.mjs file, just after the app.use(express.urlencoded({ extended: true })); line.
This will extract the tenant ID from the request params, and configure the nile client to use it as
the current tenant before handling any request.server.mjsCreate a Tenant-Aware Todos Table
In Nile console or another database client of your choice, run the following SQL to create a new table called
todos
and populate it with some example tasks.Add route
Add a route that takes a tenant Id and queries the database. if
app.param is set
(as we did in the previous step), the query will automatically be isolated to the
current tenant. See how it returns data onlyfor the tenant we requested even
if there are multiple tenants in the database and even though the query does not include a tenant_id filter.Add the following code to your server.mjs file, just after the app.delete(paths.delete, handler); line:server.mjsSecuring Routes
The route we created is isolated to a specific tenant, however at this point, any user can call it. It is not secure.
Lets protect it by checking if the user is authenticated. Add the following code to your
server.mjs file, just after the app.get("/api/tenants/:tenantId/todos", async (req, res) => { line:Test the route
First, lets try to access the route without authentication. Make sure you replace the tenantId with the one you created in the previous step.You should see the following outputNow, lets try to access the route with authentication. Make sure you replace the tenantId with the one you created in the previous step.First, authenticate the userThen, access the route, using the cookies we got in the previous stepYou should see the following output