  We prove a localization theorem for affine $W$-algebras in the spirit of
Beilinson--Bernstein and Kashiwara--Tanisaki. More precisely, for any
non-critical regular weight $\lambda$, we identify $\lambda$-monodromic
Whittaker $D$-modules on the enhanced affine flag variety with a full
subcategory of Category $\mathscr{O}$ for the $W$-algebra. To identify the
essential image of our functor, we provide a new realization of Category
$\mathscr{O}$ for affine $W$-algebras using Iwahori--Whittaker modules for the
corresponding Kac--Moody algebra. Using these methods, we also obtain a new
proof of Arakawa's character formulae for simple positive energy
representations of the $W$-algebra.
