We give a solution of the optimal transport problem in groups of type H when the cost function is the square of either the Carnot–Carathéodory or Korányi distance. This generalizes results previously proved for the Heisenberg groups. We use the same strategy that the one which was developed in that special case together with slightly refined technicalities that essentially reflect the fact that the center of the group can be of dimension larger than one. For each distance we prove existence, uniqueness and give a characterization of the optimal transport. In the case of the Carnot–Carathéodory distance we also prove that the optimal transport arises as the limit of the optimal transports in natural Riemannian approximations.