Disable REST API for Non-Admins

add_filter('rest_authentication_errors', function($result) {
    if (!empty($result)) {
        return $result;
    }
    if (!is_user_logged_in()) {
        return new WP_Error('rest_not_logged_in', 'API is not available for anonymous users.', array('status' => 401));
    }
    return $result;
});