// Stay in touch?
Products
Encore CloudEncore Cloud
Encore.tsEncore.ts
Encore.goEncore.go
PricingPricing
Book a DemoBook a Demo
Use Cases
AI-Powered DevelopmentAI-Powered Development
Event-Driven SystemsEvent-Driven Systems
Distributed SystemsDistributed Systems
Case StudiesCase Studies
ShowcaseShowcase
Resources
DocsDocs
InstallInstall
Example AppsExample Apps
Demo videoDemo video
ArticlesArticles
ResourcesResources
GitHub ReleasesGitHub Releases
Systems Operational
Company
About UsAbout Us
Swag ShopSwag Shop
ContactContact
JobsJobs
PressPress
TermsTerms
Privacy PolicyPrivacy Policy
Data Processing AgreementData Processing Agreement
Enterprise SLAEnterprise SLA
Encore
© 2026 EncoreAll rights reserved
© 2026 Encore All Rights Reserved
GitHubDiscordYouTube

Custom Middleware in Encore.ts

Launch Week Day 1

Dec 9, 2024
3 Min Read
Marcus Kohlberg
Dec 9, 2024

Custom Middleware in Encore.ts

Launch Week Day 1

Marcus Kohlberg
3 Min Read

It's Day 1 of Launch Week and we're kicking off with a highly requested feature...

Introducing: Custom Middleware in Encore.ts

You asked, we delivered! Starting today, you can define your own middleware in Encore.ts.

While common use cases like authentication, logging, and tracing are already built-in, Custom Middleware now lets you create middleware tailored to your application with ease. This is great for cases where you want to write reusable functionality that applies to multiple API endpoints.

How it works

Encore.ts makes it simple to create middleware by letting you attach middleware functions to specific services through the service definitions configuration. Middleware can be configured with a target option to specify which API endpoints it applies to, such as those requiring authentication.

Example

The simplest way to create a middleware is to use the middleware helper in encore.dev/api, here is an example of a middleware that will run for endpoints that require auth:

import { middleware } from "encore.dev/api"; export default new Service("myService", { middlewares: [ middleware({ target: { auth: true } }, async (req, next) => { // do something before the api handler const resp = await next(req); // do something after the api handler return resp }) ] });

Middleware ordering

Middleware runs in the order they are defined in the service definitions configuration, i.e:

export default new Service("myService", { middlewares: [ first, second, third ], });

Middleware forms a chain, allowing each middleware to introspect and process the incoming request before handing it off to the next middleware by calling the next function that's passed in as an argument. For the last middleware in the chain, calling next results in the actual API handler being called.

The req parameter provides information about the incoming request, it has different fields depending on what kind of handler it is.

This design enables you to add custom logic both before and after an API handler is executed, making it easy to inspect the request, modify the response, or enforce rules by throwing errors.

Targeting APIs

The target option specifies which endpoints within the service the middleware should run on. If not set, the middleware will run for all endpoints by default.

For better performance, use the target option instead of filtering within the middleware function. This enables calculating applicable middleware per endpoint during startup, reducing runtime overhead.

Learn more

Learn more in the docs or tune into today's live stream at 14:00 CET for an in-depth walkthrough and a live Q&A session.

Try it yourself

If you're already using Encore, update using encore version update.

If you're new to Encore, install using the instructions below and then create an example app using encore app create.

macOS
Windows
Linux
Brew
$ brew install encoredev/tap/encore

If you have questions or just want to hang out with other Encore developers, join our community on Discord.

Contents
Introducing: Custom Middleware in Encore.ts
How it works
Example
Middleware ordering
Targeting APIs
Learn more
Try it yourself

A development platform for your own cloud on AWS & GCP

Encore automates infrastructure management, observability, and documentation. Your team can focus on shipping product.

Encore

This blog is presented by Encore, the backend framework for building robust type-safe distributed systems with declarative infrastructure.

Like this article?
Get future ones straight to your mailbox.

You can unsubscribe at any time.

Related Articles

AI
06/03/26 / 7 Min Read
AI
06/03/26 / 7 Min Read
AI agents love type errors
An agent only fixes what it can see before it stops, and a compile error is the one signal that always lands in time.
Ivan Cernja
AI
05/20/26 / 14 Min Read
AI
05/20/26 / 14 Min Read
Are TypeScript backend frameworks ready for AI agents?
We set out to run one benchmark across five TypeScript backend frameworks. Reading the diffs sent us into two more runs, and the picture changed each time.
Ivan Cernja
Infrastructure
05/04/26 / 7 Min Read
Infrastructure
05/04/26 / 7 Min Read
What is distributed tracing?
An interactive guide to debugging real backend issues
Ivan Cernja