AdvancedLesson 24 of 305 min read

The REST API: connecting WordPress to other apps

Read and write WordPress data from other programs: endpoints, methods, status codes, application passwords and your own safe routes, with a real API test.

In this lesson you will

  • Read data with the REST API
  • Understand status codes and login
  • Write a safe custom route
  • Use the API wisely

About 17 minutes

Letting other programs talk to your site

Tomas wants a mobile app that shows the latest posts from his site, and a small tool that creates draft posts from a spreadsheet. Neither should need to log in to the dashboard and click buttons. WordPress includes a REST API: a standard way for programs to read and write site data over the web, using addresses and simple data messages. It powers the block editor itself, and it is how apps, other websites and automation tools connect to WordPress.

The basics

  • Every WordPress site has an API root at /wp-json/.
  • Content lives at addresses like /wp-json/wp/v2/posts.
  • GET reads data, POST creates it, PUT or PATCH changes it and DELETE removes it.
  • Data is exchanged as JSON, a simple text format that programs read easily.
  • Reading public content needs no login; writing needs proof of who you are.

The API, tested for real

A test created a post, read it through the API, tried to create a post without logging in, created a draft as a logged-in user and registered a small custom route. The requests were made inside WordPress, using the same code that serves outside requests. The output is real:

GET /wp/v2/posts/1102 -> status 200, title: Zz API post, keys include: id, date, date_gmt, guid, modified, modified_gmt
anonymous POST -> status 401 (rest_cannot_create)
logged-in POST -> status 201, new draft id exists: yes, status: draft
custom route -> {"hello":"Sam"}
/wp-json/ over HTTP: 200, routes listed: more than 20
cleaned: yes

Reading the post returned status 200, which means success, and the data included fields such as id, date and title. The anonymous attempt to create a post was refused with status 401 and a reason code, which is exactly what you want: strangers cannot write to your site. The logged-in attempt returned 201, ‘created’, and the draft really existed. A custom route returned a small piece of JSON, and the root address over real HTTP answered with a long list of routes.

Status codes to know

Code Meaning
200 OK
201 Created
401 Not logged in or not allowed
403 Logged in but forbidden
404 Not found

Authentication

Programs outside the browser need a way to prove who they are. WordPress supports application passwords, created per user in the profile screen, which an app sends instead of the real password. They can be revoked individually. Always use HTTPS so credentials are not readable in transit. Give the app’s user the lowest role that does the job, as in lesson 16.

Your own routes

add_action('rest_api_init', function () {
    register_rest_route('mysite/v1', '/hello', [
        'methods'             => 'GET',
        'callback'            => fn($req) => ['hello' => sanitize_text_field($req['name'] ?? 'world')],
        'permission_callback' => '__return_true',
    ]);
});

The permission_callback decides who may call the route. Here it is open to everyone because the route only reads harmless data. A route that changes data must check a capability. Leaving permissions open on a writing route is a serious security hole. Always sanitise the inputs, as the sample does.

What the API is used for

  • Mobile and desktop apps.
  • Headless sites, where WordPress stores content and another front end shows it.
  • Automation tools that create posts from forms or spreadsheets.
  • Custom blocks and admin screens that load data without refreshing.

Safety and limits

  • Do not expose private data in custom routes.
  • Consider whether visitors need the user list; some sites restrict it.
  • Add rate limits or caching for busy public routes.
  • Test routes as a logged-out visitor, as you did in the real test.

Practice

Open /wp-json/wp/v2/posts on your practice site in a browser and find the title of your latest post inside the data. Then write down which fields an app showing a news list would need. Finally list which actions in your plan need login and which are public.

Practice answer

A news list normally needs the title, date, excerpt, link and featured image. Anything that creates or changes data needs login. Writing the split on paper first prevents accidental open doors.

Common mistake

Setting a writing route’s permission to open for convenience. Anyone on the internet could then change your site.

A small worked example: a news list in an app

An app needs to show the five latest posts. It asks the API for posts with a limit of five, reads the title, date, excerpt and link from each item, and shows them in a list. When the user taps one, the app opens the link or asks the API for the full post. No login is needed because the posts are public. If the same app also lets an editor submit a draft, that action needs a login, an application password and a user whose role allows drafts only.

App action Method and login
Show latest posts GET, no login
Open one post GET, no login
Submit a draft POST, login needed
Delete a post DELETE, login and strong role

Working with JSON

JSON is plain text made of names and values, grouped with curly braces and square brackets. You can read it with your eyes, and every language has tools to turn it into ordinary variables. When you test an API, open the address in a browser or a tool that formats JSON. Look for the fields you need, and notice how some values are nested, such as the title, which carries a rendered version.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does the REST API do?

  1. Makes images smaller
  2. Changes the theme
  3. Lets programs read and write site data
  4. Backs up files

It speaks JSON over web addresses.

What did the anonymous POST return?

  1. 201, created
  2. 200, OK
  3. 404, not found
  4. 401, refused

Strangers cannot write.

What is an application password for?

  1. Making the site faster
  2. Letting an app log in without your real password
  3. Resetting themes
  4. Hiding posts

It can be revoked on its own.

What does permission_callback decide?

  1. How fast it loads
  2. The colour
  3. Who may use a route
  4. The title

Writing routes must check capability.

Where is the API root?

  1. /wp-json/
  2. /wp-admin/
  3. /images/
  4. /cache/

Posts are under /wp-json/wp/v2/posts.

Summary

  • The API root is /wp-json/.
  • GET reads; POST, PUT and DELETE change data and need login.
  • Use application passwords and HTTPS.
  • Always check permissions on writing routes.

Next lesson

Next: security hardening.

Questions

Is the REST API a security risk?

Public reading is by design. Writing requires login. Risk comes from custom routes with weak permissions.

Can I turn the API off?

You can restrict it, but the block editor and many plugins need it. Restrict specific data rather than everything.

What is headless WordPress?

WordPress stores content and the API supplies it to a separate front end. It suits some projects but adds complexity.

Progress is saved in this browser.