To call a console command from the code, you should call the run() function.

Source code (C++)

// For example, to show messages:
Console::run("show_messages 1");

Console commands (regardless of whether they were typed in the console or called from the code) cannot not be executed in the middle of the frame. Instead, they are executed in the beginning of the next frame not to interrupt the current rendering process and physics calculations.