This paper shows that the optimal sovereign lending contract is state-contingent when a government can default. It provides a theoretical basis for the specification of optimal state-contingent debt instruments (SCDIs) in countries subject to large shocks that can be observed and verified by all parties involved, such as natural disasters or global pandemics. The result is obtained as the endogenous solution to a contracting problem under time-inconsistency when a government cannot credibly commit to honor debt service obligations in all possible states of nature. It is shown that rational investors optimally offer SCDIs that include additional financing when the default constraint is binding, keeping the debtor engaged in the contractual relationship and avoiding asset loss. The debtor benefits because the contract implies net-positive financing when facing a large shock, increasing concurrent welfare, while maintaining access to financing in the future for consumption smoothing at the same terms as with precommitment. SCDIs require maintaining debt at a low level compared to the precommitment case, and also a fiscal consolidation when triggered to contain the increase in debt. Extension of the time inconsistency problem to add the taxation of capital returns shows that the optimal physical capital investment is also state-contingent.