We study the endogenous determination of corporate debt maturity in a setting with default risk. We assume that firms must access the bond market and they issue debt with a flexible structure (coupon, face value, and maturity). Initially, the firm is in a low growth/illiquid state that requires debt refinancing if it matures. Since lenders do not refinance projects with positive but small net present value, firms may be forced to default in the first phase. We call this liquidity risk. The technology is such that earnings can switch to a higher (but riskier) level. In this second phase firms have access to the equity market but they may default if this is the best option. We call this strategic default risk. In the model optimal maturity balances these two risks. We show that firms with poor prospects and firms in more unstable industries will choose shorter maturities even if it is feasible to issue longer debt. The model also offers predictions on how asset maturity, asset salability, and leverage influence maturity. Even though our model is extremely stylized we find that the predictions are roughly consistent with the evidence. Moreover, it offers some insights into the factors that determine the structure of the debt.