WordPress API integration for beginners
How to call external APIs from WordPress safely: REST basics, wp_remote_get, storing keys, rate limits, and what Security Ninja does and does not do.
Topics Beginner guides
How to call external APIs from WordPress safely: REST basics, wp_remote_get, storing keys, rate limits, and what Security Ninja does and does not do.
Topics Beginner guides
APIs let WordPress talk to other services: weather, payments, CRMs, maps, stock data. Done well, your site shows live data without hand-editing pages. Done badly, you leak API keys, hammer third-party quotas, or trust unvalidated JSON on the front end.
An application programming interface is a contract: you send a request to an endpoint, you get structured data back (usually JSON). You do not need the other company’s source code. You need their docs, auth method, and rate limits.
Common shapes:
GET, POST, and so on) against URLsWordPress also exposes an HTTP client so your PHP code can call those endpoints without reinventing sockets.
Do not mix the two in your head:
You often use both: a custom REST route on your site that, server-side, calls Stripe or a weather provider and returns a cleaned response to the browser.
Prefer the WordPress HTTP API over raw curl unless you have a specific need.
$response = wp_remote_get(
'https://api.example.com/v1/status',
array(
'timeout' => 15,
'headers' => array(
'Authorization' => 'Bearer ' . EXAMPLE_API_TOKEN,
),
)
);
if ( is_wp_error( $response ) ) {
// Log and fail closed.
return;
}
$code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
wp_remote_post() follows the same pattern for writes. Put reusable logic in a small custom plugin, not only in the theme’s functions.php, so the integration survives theme switches.
For front-end updates without full page reloads, use the REST API or admin-ajax carefully, still keeping secrets on the server.
Goal: show a short status string from https://api.example.com/v1/status without exposing the API token to browsers.
wp-config.php (or the host env), not in the database options screen:define( 'EXAMPLE_API_TOKEN', 'replace-me' );
function example_get_remote_status() {
$cached = get_transient( 'example_api_status' );
if ( false !== $cached ) {
return $cached;
}
$response = wp_remote_get(
'https://api.example.com/v1/status',
array(
'timeout' => 15,
'headers' => array(
'Authorization' => 'Bearer ' . EXAMPLE_API_TOKEN,
),
)
);
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return '';
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $data ) || empty( $data['status'] ) ) {
return '';
}
$status = sanitize_text_field( $data['status'] );
set_transient( 'example_api_status', $status, 5 * MINUTE_IN_SECONDS );
return $status;
}
add_shortcode(
'example_status',
function () {
$status = example_get_remote_status();
if ( '' === $status ) {
return '<p>Status unavailable.</p>';
}
return '<p>Status: ' . esc_html( $status ) . '</p>';
}
);
Optional REST wrapper if JavaScript needs the value: register a custom route that calls the same helper and returns JSON. Still keep EXAMPLE_API_TOKEN only in PHP. Check capabilities if the data is not public.
Operate it: log failures, watch provider rate limits, rotate the token when people leave, and delete the plugin when the integration dies.
Swap in your real endpoint and field names. The security shape stays the same: secret on the server, validate JSON, escape HTML, cache when you can.
No-code API plugins (for example WPGetAPI-style tools) help non-developers map endpoints in wp-admin. Custom code gives you control, tests, and clearer security review.
Either way:
Protect keys. Do not hardcode secrets in public repos or paste them into page content. wp-config.php constants, environment variables, or a secrets store beat committing tokens to Git.
Least privilege. Use scoped API keys. Revoke keys you no longer need. See least privilege.
Validate responses. Never trust remote JSON blindly. Check HTTP status codes. Escape output when rendering HTML.
Rate limit and cache. Call APIs on cron or on demand with transients, not on every anonymous page hit, unless the provider and your host can take it.
Error handling. Failed calls should degrade gracefully, not white-screen the site.
WP Security Ninja helps with site security posture: tests, hardening guidance, monitoring modules, and related tools. It is not an API connector builder and it does not “auto-integrate” Stripe or weather feeds for you.
Use Security Ninja to keep the WordPress install harder to abuse while your integration code (or a dedicated API plugin) handles the external calls. Broader baseline: WordPress security checklist.
wp_remote_get / wp_remote_post, parse JSON, handle errorsAPIs make WordPress more useful. Treat every key like a password and every remote payload like untrusted input, and most integration pain stays boring.
Found this useful? Share it.