Creating the API using Cloud Endpoints Frameworks for Python
Stay organized with collections
Save and categorize content based on your preferences.
An API is a remote procedure call (RPC) service that provides remote
methods accessible to external clients. Each backend API consists of an RPC
service class that subclasses the
ProtoRPC remote.Service class, and
one or more methods. When you define a method, you must also define
Message classes
for the requests coming into that method and the responses returned by it.
A Message class performs a mapping function so the incoming data can be
extracted and supplied to the service method properly, or supplied properly to
the outgoing response.
If a request has path or query string arguments, you use a
ResourceContainer
class for the mapping, instead of a simple Message class.
Finally, you need to decorate the API service class and class methods, and you
need to define Message classes for the requests and responses.
Creating the API
The following procedure shows how to decorate your code to create an API
implemented in a single class. If you have a multi-class API, see
Creating an API implemented with multiple classes.
See
Decorators
for detailed information about all the available decorators.
Notice that your API name and the name of your service class don't need to
be the same. The version number applies to the version of the API. The value
that you enter becomes part of the path in the URL to your API. For more
information on versions, see
Handling API versioning.
Determine what data your method expects from the request and what data is
returned, and create a
Message class
for the request body and response body:
classEchoRequest(messages.Message):message=messages.StringField(1)classEchoResponse(messages.Message):"""A proto Message that contains a simple string field."""message=messages.StringField(1)ECHO_RESOURCE=endpoints.ResourceContainer(EchoRequest,n=messages.IntegerField(2,default=1))
Note that if no arguments appear in the request body, such as in a GET
request, you can omit the Message class for the request and simply use the
value message_types.VoidMessage.
If your request has path or query string arguments, replace
your Message class with an appropriate
ResourceContainer.
For complete information on forming and using Message classes, see the
documentation for the
Google Protocol RPC
response and request Message classes.
Create the method for your API, and decorate it with
@endpoints.method:
@endpoints.method(# This method takes a ResourceContainer defined above.ECHO_RESOURCE,# This method returns an Echo message.EchoResponse,path="echo",http_method="POST",name="echo",)defecho(self,request):
If your request has path or query string data, replace the request message
type with an appropriate
ResourceContainer.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-21 UTC."],[[["\u003cp\u003eAn API is an RPC service with remote methods accessible to external clients, requiring a service class that subclasses \u003ccode\u003eremote.Service\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eEach API method needs \u003ccode\u003eMessage\u003c/code\u003e classes defined for both incoming requests and outgoing responses to properly map data, though these \u003ccode\u003eMessage\u003c/code\u003e classes are not part of the exposed API.\u003c/p\u003e\n"],["\u003cp\u003eIf requests have path or query string arguments, a \u003ccode\u003eResourceContainer\u003c/code\u003e class should be used instead of a simple \u003ccode\u003eMessage\u003c/code\u003e class for data mapping.\u003c/p\u003e\n"],["\u003cp\u003eTo create an API, you must decorate a subclass of \u003ccode\u003eremote.Service\u003c/code\u003e with \u003ccode\u003e@endpoints.api\u003c/code\u003e, define \u003ccode\u003eMessage\u003c/code\u003e or \u003ccode\u003eResourceContainer\u003c/code\u003e classes for requests and responses, and decorate the method with \u003ccode\u003e@endpoints.method\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe API's name and version, specified in the \u003ccode\u003e@endpoints.api\u003c/code\u003e decorator, are incorporated into the API's URL path, allowing for version management.\u003c/p\u003e\n"]]],[],null,[]]