Where to put API Code?

You can create a plugin to contain your code. The admin_post_ action can be used to trigger a request handler. WordPress core will be loaded, and the API will work.

<?php
/*
Plugin Name: Ben's GF Plugin
*/

// for logged-in users
add_action( 'admin_post_ben_gf', 'ben_gf_handle_request' );
// for non logged-in users
add_action( 'admin_post_nopriv_ben_gf', 'ben_gf_handle_request' );

function ben_gf_handle_request() {

    // Your processing code.
    // WordPress functions will work here.
    echo 'Your home url: ' . home_url();

    // die() at the end to terminate execution
    die();
}

This will map your request handler to the URL:

http://www.example.com/wp-admin/admin-post.php?action=ben_gf

Use caution if you are allowing non-logged in users to access this URL, you don’t want to publicly expose sensitive data.