API Routes

By design, Vike doesn't have and doesn't plan to include built-in support for API routes. (We believe API solutions should be developed independently of the frontend framework.)

See Guides > Data Fetching for a general introduction about fetching data with Vike.

Instead use:

  • Telefunc

    Telefunc is a simple solution providing a much improved DX over API routes. Out of all options, we recommend Telefunc most.

  • tRPC

    Similar to Telefunc.

    • Con: Telefunc is simpler.
    • Pro: supports real-time endpoints (using websockets).
  • You can use a server, such as Express.js or Hono, in order to create new HTTP GET/POST routes, providing a DX similar to that of API routes.

    In general we recommand using Telefunc instead of API routes, but using API routes can make more sense:

    • If you only need a handful of endpoints, creating a couple of server routes can be simpler than using Telefunc and thus adding yet another tool to your stack.
    • For specific needs where Telefunc doesn't fit (most notably file upload), you can use API routes (in addition to Telefunc).

    If you want to create such API routes then:

    • Make sure Vike's middleware is your last middleware. (Vike's middleware is a catch-all middleware and would therefore override your API routes.)
    • When modifying your server, you may need to manually restart your server for your changes to take effect. (See #562.)
  • GraphQL

    ⚠

    If you aren't a very large team, then GraphQL is most likely over-engineering. RPC (such as Telefunc and tRPC) isn't only simpler but actually also more flexible (as long as you aren't a very large team).

    We recommend getting started with RPC and use GraphQL only later if/when the need arises.

See also