REST API
The admin screens run on a REST API you can use too. It has 12 routes under the namespace nahnu-fonts/v1, for example https://example.com/wp-json/nahnu-fonts/v1/fonts.
Authentication#
Every route needs a user with the manage_options capability (change it with the nahnu_fonts_capability filter), and one of:
- a REST nonce (
X-WP-Nonceheader) for requests from the WordPress admin, or - an application password for requests from outside.
curl -u admin:"xxxx xxxx xxxx xxxx xxxx xxxx" \
https://example.com/wp-json/nahnu-fonts/v1/fonts Routes#
| Method | Route | Body or query | Does |
|---|---|---|---|
| GET | /fonts | search, source | The library, admin preview CSS and build info |
| GET | /fonts/{id} | none | One font (stored and public shape) |
| POST | /fonts/{id} | name, fallback, active, selector, preload[], remove_faces[] | Update a font |
| DELETE | /fonts/{id} | none | Delete a font and its files |
| POST | /fonts/{id}/redownload | none | Download a Google font again through the current source |
| GET | /catalog | refresh | The Google Fonts list from the chosen source |
| POST | /google | family, weights[], italic, subsets[], variable | Download and self-host a family |
| POST | /custom | multipart: name, fallback, id, faces (JSON), files | Upload custom font files. Pass id to add to an existing family |
| POST | /adobe | project_id | Connect an Adobe Fonts project |
| DELETE | /adobe | none | Disconnect it |
| GET, POST | /settings | settings | Read or save settings |
| POST | /rebuild | none | Rebuild the stylesheet |
| POST | /import | multipart: file | Import an export .zip |
| GET | /jooosi | none | What Jooosi Fon data exists to move |
| POST | /jooosi/migrate | none | Move fonts and settings from Jooosi Fon |
Export#
Export isn’t a REST route, because it streams a file. Use:
wp-admin/admin-post.php?action=nahnu_fonts_export&_wpnonce=...[&fonts=inter,lora] The nonce comes from wp_create_nonce( 'nahnu_fonts_export' ). Leave out fonts to export everything. From the command line, wp nahnu-fonts export is easier.
Example: add a Google Font#
curl -u admin:"xxxx xxxx xxxx xxxx xxxx xxxx" \
-H "Content-Type: application/json" \
-d '{"family":"Inter","weights":["400","700"],"italic":false,"subsets":["latin"]}' \
https://example.com/wp-json/nahnu-fonts/v1/google Errors#
Errors are standard WordPress REST errors with a code, a readable message and an HTTP status, for example 400 for bad input or 404 for an unknown font. Every error also fires the nahnu_fonts_rest_error action, handy for logging.
Adding routes#
Use the nahnu_fonts_rest_routes filter to add or change routes in the same namespace, with the same permission check. See Hooks.