We propose a new methodology for estimating the demand and cost functions of differentiated products models when demand and cost data are available. The method deals with the endogeneity of prices to demand shocks and the endogeneity of outputs to cost shocks, by using variation in market size that does not need to be exogenous, and cost data. We establish nonparametric identification, consistency and asymptotic normality of our estimator. Using Monte-Carlo experiments, we show our method works well in contexts where instruments are correlated with demand and cost shocks, and where commonly-used instrumental variables estimators are biased and numerically unstable.