My website is fully API based and builds pages/posts from API data. I was thinking to create something like PHP magic function __call to call specific function when requested URL would return 404, but do it before it happens, so I could send API request and create the page on the fly and return the previously missing page to user.
What are the options to do it? I know wp_insert_post is the way to add new post via PHP, but I can't find how to hook into initial request before 404 is thrown to initiate wp_remote_request.
Basically I think I miss the filter/action name for it.
Thanks for any help.