// 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
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
Encore

The backend platform for humans and agents

Build backends in Go or TypeScript and run them on your own AWS or GCP, with the guardrails your team and AI agents need to ship safely.

Get started
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

Development
07/15/26 / 7 Min Read
Development
07/15/26 / 7 Min Read
We compiled our TypeScript parser to WASM
How we compiled the Rust parser that reads infrastructure out of Encore code so it runs in the browser, and how we use it to reproduce the parser errors people report.
Ivan Cernja
AI
07/07/26 / 7 Min Read
AI
07/07/26 / 7 Min Read
Automating cloud infrastructure with Cursor
Now a Cursor agent can provision your cloud infrastructure from the same code it writes, on your own AWS or GCP.
Ivan Cernja
Development
06/25/26 / 6 Min Read
Development
06/25/26 / 6 Min Read
We put a Redis server inside our runtime
How we run an in-memory Redis inside the runtime for local development and tests, and how we keep it behaving the same as the Redis you run in production.
Ivan Cernja