Improve error handling and logging in /api urls

Fixes #29