I suppose the correct way of doing it is to get the action by taking the integral of the Lagrangian then using the principle of stationary action.
Agreed. One 'problem' with the lagrangian is that it contains potentials which are not unique: you can add any constant to a potential and it does not change the result of minimising the action (i.e gauge invariance). That is because one minimises by differentiating, and a constant term always goes to zero on differentiation. So if you are not minimising the action, the lagrangian could give you any answer you wanted by adding an arbitrary constant to the potential. Not sure that your X is a constant, but whatever it is it would need to preserve gauge invariance, and a constant would do that OK.