In most countries, suppliers of intermediate goods are also the main providers of short term financing to their customers via trade credit contracts. This paper builds a model where these inter-firm financial linkages arise endogenously and uses it to understand how trade credit responds to financial shocks and affects their propagation. In the model, trade credit is the outcome of a long-term contract between firms in a production line: downstream firms have incentives to repay their debt when the relationship with their supplier is valuable, and the higher is the value of such relationship the more trade credit the pair can sustain. In addition, production lines that can sustain more trade credit are better able to smooth the impact of financial shocks, provided that suppliers have enough borrowing capacity themselves. Using Italian data, we show that the model can rationalize the observed differences across industries in trade credit and in their performance during the Great Recession.