We present a global solution for an optimal feedback control problem of the underactuated Heisenberg system or nonholonomic integrator. Set in the general framework of the Hamilton-Jacobi theory, this work demonstrates the potential applicability of our methodology to general underactuated optimal control problems. We incorporate the Heisenberg system into a typical optimal control formulation called the hard constraint problem, and transform into a two point boundary value problem for a Hamiltonian system. It is viewed as a canonical transformation in itself, to which we apply our recently developed technique based on generating functions appearing in the Hamilton-Jacobi theory. It is first recognized that our previously developed procedure for solving fullyactuated optimal control problems is not directly applicable due to a singularity caused by underactuation. However, within the same framework of generating functions we are provided with a way to circumvent this singularity by algebraic manipulations linked with the underactuated coordinate. This results in a scalar transcendental equation whose solution ultimately leads to a nonlinear optimal feedback control law in an analytical form. We illustrate our solution by numerical examples.