We study the joint dynamics of foreign capital flows and real activity during the recent boom-bust cycle of the Spanish economy, using a three-country New Keynesian model with credit-constrained households and firms, a construction sector and a government. We estimate the model using 1995Q1-2013Q2 data for Spain, the rest of the Euro Area (REA) and the rest of the world. We show that falling risk premia on Spanish housing and nonresidential capital, a loosening of collateral constraints for Spanish households and firms, as well as a fall in the interest rate spread between Spain and the REA fuelled the Spanish output boom and the persistent rise in foreign capital flows to Spain, before the global financial crisis. During and after the global financial crisis, falling house prices, and a tightening of collateral constraints for Spanish borrowers contributed to a sharp reduction in capital inflows, and to the persistent slump in Spanish real activity. The credit crunch was especially pronounced for Spanish households; firm credit constraints tightened later and more gradually, and contributed much less to the slump.