In the mainline games, Darkrai has almost always been event-exclusive — which means the honest answer is that it depends entirely on which game you're playing.
Darkrai is a Mythical Pokémon, and Mythical Pokémon are deliberately kept out of normal gameplay. Across the generations it has been distributed through official events, and in some titles there are in-game routes to encounter it — Pokémon Legends: Arceus and Brilliant Diamond/Shining Pearl both offer ways to obtain it under certain conditions.
Any site claiming a code or trick that generates a Darkrai out of nothing is almost certainly a scam or a save-editing tool in disguise. If you're after one legitimately, check what your specific game offers, and keep an eye on official distributions.