We prove that a Pfaffian system with coefficients in the critical space $L^2_\mathrm{loc}$ on a simply connected open subset of $\mathbb{R}^2$ has a non-trivial solution in $W^{1,2}_\mathrm{loc}$ if the coefficients are antisymmetric and satisfy a compatibility condition. As an application of this result, we show that the fundamental theorem of surface theory holds for prescribed first and second fundamental forms of optimal regularity in the classes $W^{1,2}_\mathrm{loc}$ and $L^2_\mathrm{loc}$, respectively, that satisfy a compatibility condition equivalent to the Gauss–Codazzi–Mainardi equations. Finally, we give a weak compactness theorem for surface immersions in the class $W^{2,2}_\mathrm{loc}$.