Using WSL with FastAPI

FastAPI is accessing the web application from a Windows browser. However, there is a solution: npx wsl-expose. This tool allows you to easily expose a port from your WSL environment to your Windows environment.

To use npx wsl-expose, follow these steps:

  1. Install Node.js on your Windows machine, if it is not already installed.
  2. Open a WSL terminal and install npx by running the command npm install -g npx.
  3. In the WSL terminal, run the command npx wsl-expose to expose the specified port to your Windows machine.
  4. Start your FastAPI application as you normally would, specifying the host as 0.0.0.0 and the port as any available port: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
  5. Access your FastAPI application by navigating to http://localhost:<port> in your Windows browser. And in other devices of local network use the windows ip address.
  6. When you need to kill uvicorn you can use: sudo lsof -t -i tcp:8000 | xargs kill -9