We consider an optimization problem arising in the context of gas transport in pipe networks. To compensate the pressure loss due to friction and to guarantee a desired (time dependent) outflow profile, compressor stations are included in the network. These compressor stations are relatively cost-intensive, so that a cost effective control is required. In the presented model the compressors are special vertices of the network. We derive an adjoint calculus for gas networks to solve the optimization problem and prove well–posedness of forward and adjoint coupling conditions. Furthermore, numerical examples illustrate the obtained results.