When should I call add_settings_section and add_settings_field?

You can call add_settings_section and add_settings_field any time before you do_settings_sections:

add_action( 'my_plugin_add_settings_sections', function() {
    wp_remote_get( ... );
    add_settings_section( ... );
    add_settings_field( ... );
});

// On the plugin settings page output
do_action( 'my_plugin_add_settings_sections' );
do_settings_sections( ... );

You’ll still want to register_setting() earlier though because that custom hook won’t be fired when your settings are saved, so you want to make sure sanitize callback is in place, probably admin_init.

error code: 523