Mechanisms according to which private intermediaries or governments charge transaction fees or indirect taxes are prevalent in practice. We consider a setup with multiple buyers and sellers and two-sided independent private information about valuations. We show that any weighted average of revenue and social welfare can be maximized through appropriately chosen transaction fees and that in increasingly thin markets such optimal fees converge to linear fees. Moreover, fees decrease with competition (or the weight on welfare) and the elasticity of supply but decrease with the elasticity of demand. Our theoretical predictions fit empirical observations in several industries with intermediaries.