Dittofi
  • Tutorials
    • Tutorial Index
    • Quick Start
    • Dittofi Essentials
      • Part 1: Frontend terms and concepts
      • Part 2: Backend terms and concepts
      • Part 3: Connecting frontend and backend
      • Part 4: Dittofi app structure
    • Dittofi App Design
      • 🏗️App architecture 101
      • ◼️Backend app development 101
      • 🟪Frontend app development 101
      • 🔰(optional) Web developer beginner series
  • Learn Frontend
    • 👨‍💻Getting Started
      • Welcome to Dittofi
      • Intro to the Dittofi Design Studio (Frontend)
    • 🟩Pages
    • 🟪Elements
      • Types
        • Button Element
        • Div Block Element
        • Form Block Element
        • Link Block Element
        • List Element
        • Modal Box Element
        • Tabs Element
        • Columns Element
        • Image Element
      • Partials
      • Prefabs
      • Custom
    • 🟧Variables
      • Array
      • Checkbox
      • Collection
      • Collection (Custom Model)
      • Collection (System Custom Model)
      • Custom Model
      • Date
      • Datetime
      • File
      • Json
      • Number
      • Number with decimal
      • Model
      • System Custom Model
      • Text
      • UUID
    • 🟨Events
      • 🟨External
        • HTTP Request
        • Run Endpoint
      • 🟨Flow Control
        • Condition
        • Run Action
        • Loop
        • Sleep
      • 🟨Maths
        • Add
        • Divide
        • Modulus
        • Multiply
        • Subtract
      • 🟨Navigation
        • Open Page
      • 🟨Notifications
        • Alert
        • Confirm
      • 🟨Other
        • Copy To Clipboard
        • Run Custom
      • 🟨Text
        • Concat
        • Contains
        • Ends With
        • I Contains
        • I Ends With
        • I Starts With
        • LTrim
        • RTrim
        • Text To Upper
        • Text To Lower
        • Trim
        • Starts With
      • 🟨Variables
        • Set Field
        • Remove Field
      • Validate
    • 🟦CSS Layouts
      • Holy Grail Layout
      • Side Bar Layout
      • Sticky Footer Layout
      • Sticky Header Layout
      • CSS Frameworks
    • 🔲CSS Frameworks
    • Component Libraries
    • Meta Attributes
  • Learn Backend
    • 🖥️Getting Started
      • Intro to Data
      • Intro to APIs
      • Build your first custom API
      • Actions & Events
    • 🟫Back-End Actions
    • 🟩Custom Models
      • 🟩Attributes
        • Array
        • Checkbox
        • Custom Model
        • Collection (Custom Model)
        • Collection (System Custom Model)
        • Date
        • Datetime
        • File
        • Number
        • Number With Decimal
        • Json
        • Text
    • ◼️Data Models
      • Database Fundamentals
        • Database relationships
      • ◼️Columns
        • Attachment
        • Auto Increment
        • Checkbox
        • Date
        • Datetime
        • Duration
        • Email
        • Long text
        • Link to another record field
        • Number
        • Number with decimal
        • Password
        • Phone number
        • Percent
        • Price
        • Rating
        • Single select
        • Single line text
        • URL
    • 🟥Events
      • 🟦Arrays
        • Append
        • Array Pop
        • At
        • Length
        • Prepend
        • Range
      • 🟦Conversions
        • Base64 Encode
        • Base64 Decode
        • Json Decode
        • Json Encode
        • Path Escape
        • Path Unescape
        • Query Escape
        • Query String Parse
        • Query Unescape
        • To Bool
        • To Float
        • To Int
        • To Str
        • Yaml Encode
        • Yaml Decode
      • 🟦Database Requests
        • Create
        • Create Or Update
        • Delete
        • Has Record
        • Read Event
        • Update
      • 🟦External
        • HTTP Request
        • SFTP Download
        • SFTP Upload
      • 🟦Files
        • CSV Reader Event
        • CSV Writer Event
        • File Read
        • File Save
        • File Size
        • File Type
        • JSON Reader
        • PDF Generator
      • 🟦Flow Control
        • Condition
        • Loop
        • Return
        • Run Action
        • Sleep
      • 🟦Notifications
        • Send Mail
        • Connect Websocket
        • Message Websocket
      • 🟦Maths
        • Add
        • Acos
        • Acosh
        • Asin
        • Asinh
        • Atan
        • Atanh
        • Cbrt
        • Ceil
        • Divide
        • Modulus
        • Multiply
        • Subtract
      • 🟦Security
        • Compare Password
        • JWT Decode
        • JWT Encode
        • Login
        • Logout Web User
        • Password Generator
        • Random Number
      • 🟦Time
        • Add Time
        • Day
        • Format Time
        • Minute
        • Nanosecond
        • Parse Time
        • Second
        • Time In
        • Year
        • Year Day
      • 🟦Text
        • Concat
        • Contains
        • Ends With
        • Execute Template
        • I Contains
        • I Ends With
        • I Starts With
        • Starts With
        • LTrim
        • RTrim
        • Replace
        • Regex Find All String
        • Regex Find String
        • Regex Match String
        • Trim
        • Text To Lower
        • Text To Upper
        • Text To Title
        • Url Getarg
      • 🟦Other
        • Assign
        • Run Custom
        • Redirect Web Page Event
        • Render Web Template Event
    • 🟧Variables
      • Array
      • Checkbox
      • Collection
      • Collection (Custom Model)
      • Collection (System Custom Model)
      • Custom Model
      • Date
      • Datetime
      • File
      • Json
      • Number
      • Number with decimal
      • Model
      • System Custom Model
      • Text
      • UUID
    • 🟪Webservices
  • Templates
    • Two sided marketplace template
      • Introduction
        • Part I: Installing and setting up the marketplace template
        • Part II: Understanding what features are included in the marketplace template
        • Part III: Customizing your marketplace frontend without coding
        • Exploring Dittofi's marketplace API
      • Listings
        • How to create a marketplace listing
    • How to build a CRUD app
  • Third Party APIs
    • OAuth 2.0 APIs
      • Introduction To OAuth 2.0
      • Google OAuth 2.0 - PART I
      • Google OAuth 2.0 - PART II
  • Dittofi Admin
    • Your Account
      • Workspace Admin
      • Manage Billing
      • Connect a custom domain
      • How to use a discount code to Dittofi Pro
  • FAQ
    • API Generator
    • How to write custom SQL?
    • Why does my image not display?
    • How can I set up Google Analytics?
    • Exporting and deploying my frontend code
    • What is a partial and what is a prefab?
    • How to set up a basic mulit tenant app on Dittofi
    • How to write a custom endpoint in Dittofi
    • How to integrate Font Awesome into Dittofi
    • How can I set up Dittofi to run on my own private server?
    • How to add roles such as Super Admin, Admin, Manager & User
    • How to prevent duplicate records being added to the database
    • What to do when relation cannot be created because their is violating data in the referenced table
  • Change Logs
    • Dittofi Change Logs
Powered by GitBook
On this page
  • 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

Was this helpful?

  1. FAQ

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.

PreviousHow to set up a basic mulit tenant app on DittofiNextHow to integrate Font Awesome into Dittofi

Last updated 2 years ago

Was this helpful?

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 .

WHY CUSTOM CODE?

There are two main reasons that you might want to use custom code in the Dittofi no-code platform:

  1. To add functionality to your backend where, using custom code would be easier than using the out of the box event types

  2. 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

Step 1. Create an endpoint

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.

Step 2. Add a new template

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:

  1. tag the template with "// @custom".

  2. Import the package "main"

  3. Import package "net/http"

  4. Import package "utils"

Your final template should look like the screenshot below.

You can copy and paste this directly from the code below.

// @custom
package main

import (
	"net/http"
  	"utils"
)

Step 3. Add your custom code

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.

// @custom
// @endpoint
func customEndpoint(w http.ResponseWriter, r *http.Request){

}

** IMPORTANT **

Your custom coded endpoint MUST:

  1. Have the same name as the endpoint that we created in Step 1. of this article.

  2. 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.

Step 4. Build your code and test

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.

(Optional) review code documentation

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.

Run Custom Code Event