Add throws InvalidOperationException and fix duplicate apiKey check