Update meta_value in wp_postmeta from an API response

I’m making an API call using the wp_remote_get method to fetch data from an external source (not a WP site). The API call is working and i’m getting back the response i want which is just a number. Now i want to store this number (the response) as a meta_value in wp_postmeta with the following meta_key (donation_amount) and a specific post_id. The database table already exists. What i have so far is the API Call and the response. I’ve tried wp_update_post but maybe i am doing something wrong or maybe is not the right approach. Any help would be appreciated.

$url = 'https://developer.mozilla.org/en-US/docs/Web/API/URL_API'; // Not the real url

$response = wp_remote_get( $url );

if( is_wp_error( $response ) ) {
    return false;
} else {
    $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}


echo $api_response ['theNumber'];

Answer

Use WP update_post_meta function. check the below code.

$url = 'https://developer.mozilla.org/en-US/docs/Web/API/URL_API'; // Not the real url

$response = wp_remote_get( $url );

if( is_wp_error( $response ) ) {
    return false;
} else {
    $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}

$donation_amount = $api_response['theNumber'];

replace $post_id with your post id.

update_post_meta( $post_id, 'donation_amount', $donation_amount );