Working with endpoints
Endpoints are the connection points to the server from the client. With Serverpod, you add methods to your endpoint, and your client code will be generated to make the method call. For the code to be generated, you need to place your endpoint in the endpoints directory of your server. Your endpoint should extend the Endpoint
class. For methods to be generated, they need to return a typed Future
, and its first argument should be a Session
object. The Session
object holds information about the call being made and provides access to the database.
Endpoints are the connection points to the server from the client. With Serverpod, you add methods to your endpoint, and your client code will be generated to make the method call. For the code to be generated, you need to place the endpoint file anywhere under the lib directory of your server. Your endpoint should extend the Endpoint
class. For methods to be generated, they need to return a typed Future
, and its first argument should be a Session
object. The Session
object holds information about the call being made and provides access to the database.
import 'package:serverpod/serverpod.dart';
class ExampleEndpoint extends Endpoint {
Future<String> hello(Session session, String name) async {
return 'Hello $name';
}
}
The above code will create an endpoint called example
(the Endpoint suffix will be removed) with the single hello
method. To generate the client-side code run serverpod generate
in the home directory of the server.
On the client side, you can now call the method by calling:
@@ -25,6 +25,10 @@Passing p
You can also pass List
and Map
as parameters, but they need to be strictly typed with one of the types mentioned above. For Map
, the keys must be non-nullable strings. E.g., Map<String, int?>
is valid, but Map<int, String>
is not.
While it's possible to pass binary data through a method call and ByteData
, it is not the most efficient way to transfer large files. See our file upload interface. The size of a call is by default limited to 512 kB. It's possible to change by adding the maxRequestSize
to your config files. E.g., this will double the request size to 1 MB:
maxRequestSize: 1048576
Return types
-The return type must be a typed Future. Supported return types are the same as for parameters.