Skip to main content

How to add routes

First we create a new folder called routes in the src directory. And make a new file hello-world

import { methods, Route, type ApiRequest, type ApiResponse } from '@sapphire/plugin-api';

export class UserRoute extends Route {
public constructor(context: Route.Context, options: Route.Options) {
super(context, {
...options,
route: 'hello-world'
});
}

public [methods.GET](_request: ApiRequest, response: ApiResponse) {
response.json({ message: 'Hello World' });
}

public [methods.POST](_request: ApiRequest, response: ApiResponse) {
response.json({ message: 'Hello World' });
}
}

A quick glance on what we did above:

  • First we extended the Route class to make our route
  • Second we specified the route name in the constructor
  • Third we defined the GET method
  • Finally we responded to the request with a message which we would be able to use later when we fetch the route