Deploy FastAPI service with a firewall (via cloudflared)

Protect a web service of any kind (like FastAPI) behind a cloudflare tunnel

Deploy FastAPI service with a firewall (via cloudflared)

Just deployed

Just deployed

Deploy and Host FastAPI service with a firewall (via cloudflared) on Railway

I loved using railway to host my api, it probably took me 30 minutes. The only thing I didn't know how to do though was blocking undesired traffic (the internet's free pen-testing kit).

So I looked up how to do it and didn't find any guide about it, so I wrote a guide. I used this template for the guide and you can re-use it to set up your own, secured api. This is the link.

About Hosting FastAPI service with a firewall (via cloudflared)

The only thing you'll need is a cloudflare account. Setting up the tunnel and the cloudflared service is easy. You might need to modify your HOST variable (::) or your Dockerfile's ENTRYPOINT so the app can be properly reached via railway's internal network (IPv6).

Common Use Cases

  • You want your app to be only accessible in a couple countries or even a restricted set of IPs.
  • You want to rate limit certain endpoints but aren't sure how to go about it in your code.
  • You're worried bots will hassle your app and you want to put at least a couple anti-bots measures in place

Dependencies for FastAPI service with a firewall (via cloudflared) Hosting

  • Cloudflare
  • Whatever dependencies your app needs.

Deployment Dependencies

Look above

Implementation Details

If you need a step by step of setting this up, refer to the guide. Also feel free to hit me up on there or commenting on this community post.

Why Deploy FastAPI service with a firewall (via cloudflared) on Railway?

Because railway is incredibly easy to manage and great value for your (multiple) bucks. This is just one way to manage traffic into your railway deployment, however I believe in great companies, which is why I think this combination is ideal.

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying FastAPI service with a firewall (via cloudflared) on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.

Template Content

More templates in this category

View Template
caring-vibrancy
Deploy and Host caring-vibrancy with Railway

View Template
Mellow Vue
A full-stack Vue app with The Boring JavaScript Stack.

View Template
NextJS 15.5 (Server Actions) with Shadcn
Better-Auth Ready with Auth flows and Server actions (Prod ready)