A variational formulation of stochastic thermodynamics. Part I: Finite-dimensional systems | Synapse