A specific API, returns an image in binary in the data.
If I need to save it to the server, I can easily do it by following the code.
$response = curl_exec($curl); // This is the API response file_put_contents('example2.png', $response);
How can I add it to the WordPress media library?
attachment, what you're asking about is called sideloading in the WordPress API. Just Putting images in the uploads folder on its own won't make them show up in the media library, and it's easier to give WP a file from a temporary folder that it can move into the preferred location by itself and generate the various image sizes
media_handle_sideload, there is no reason to download a file here