  We provide a systematic treatment of $N$-photon transport in a waveguide
coupled to a local system, using the input-output formalism. The main result of
the paper is a general connection between the $N$-photon S matrix and the Green
functions of the local system. We also show that the computation can be
significantly simplified, by exploiting the connectedness structure of both the
S matrix and the Green function, and by computing the Green function using an
effective Hamiltonian that involves only the degrees of freedom of the local
system. We illustrate our formalism by computing $N$-photon transport through a
cavity containing a medium with Kerr nonlinearity, with $N$ up to 3.
