How to write a custom endpoint in Dittofi
This article looks at how you can write a custom endpoint and add it to your Dittofi app.
Last updated
This article looks at how you can write a custom endpoint and add it to your Dittofi app.
Last updated
The run custom code event allows you to add custom code into your Dittofi backend. Your custom backend code must be written in Google GO.
You can write to types of custom code (1) custom functions and (2) custom endpoints. In this user guide we are going to look at how to write custom endpoints inside Dittofi.
To learn how to write a custom coded function and trigger this from inside an action, read Run Custom Code Event.
WHY CUSTOM CODE?
There are two main reasons that you might want to use custom code in the Dittofi no-code platform:
To add functionality to your backend where, using custom code would be easier than using the out of the box event types
To create some functionality that is very specific to your app and where the event type does not already exist inside Dittofi. For example, many clients use the custom code section to write custom analytics.
You can add custom coded endpoint to your app in 5 quick and easy steps.
Step 1. Create an endpoint
Step 2. Add a new template
Step 3. Add your custom code
Step 4. Build your code and test
(Optional) review code documentation
To create an endpoint, go to the endpoints tab inside Dittofi and click . Next, give your endpoint a name and a path, in our case we name our endpoint "customEndpoint" and give it the path "/v1/custom_endpoint", as shown below.
Remember to save your endpoint and head over to the templates tab to add the custom code for your endpoint.
You can add a custom coded endpoint from within the templates tab inside the Dittofi Design Studio. The templates tab is under the wrench icon in the purple side panel, as shown below.
From inside the templates tab, you must first add a new template into the folder Backend > Templates.
Next you need to complete the following steps to set up your custom template:
tag the template with "// @custom".
Import the package "main"
Import package "net/http"
Import package "utils"
Your final template should look like the screenshot below.
You can copy and paste this directly from the code below.
Next, we are going to add the custom code for our endpoint. This custom code needs to have the same name as the endpoint inside our Dittofi app, as you can see below.
Note, you also need to tag this as "// @custom" and also as "// @endpoint". This will tell the system that you are writing a custom endpoint. Also note that the arguments for the customEndpoint are completely standard arguments that are used in Google GO. These make use of the http library that we imported in Step 2.
You can copy and paste the endpoint function and arguments from the block of code below.
** IMPORTANT **
Your custom coded endpoint MUST:
Have the same name as the endpoint that we created in Step 1. of this article.
Be tagged with "// @custom" and "// @endpoint".
Next, let's take a look at the auto generated endpoints in Dittofi. You can see examples of these in the folder marked "Generated" inside your templates page.
If you scroll to the bottom of these endpoints, you will notice that they all they all return the same argument.
We can take this argument and copy and paste it at the bottom of our custom coded endpoint and replace the response with something like the string "Test custom endpoint" as shown below.
Open up the endpoint and click the run button in the top right of the endpoint as shown below.
You can see in the body of the endpoint response the words "Test custom endpoint" which is what we added in Step 3.
Dittofi auto generates real source code for your apps. To help stress this point, if you open up the help panel by clicking on the question mark icon inside your help panel, you will see the option for "Code Documentation".
Opening this up, you can see that the code that has generated now includes the custom function "customEndpoint". This is the custom function that we just wrote.
If you scroll down the code documentation, you can see the custom code that has been injected.
You can also use the Dittofi code export function to get your full stack code and read and add to your go code directly in there.
Finally, save your custom coded endpoint by clicking .
Next, we are going to build our code and test it. To build the code click on . Next, head on over to the customEndpoint Endpoint that we created in step 1 of this article.