# function

### <mark style="color:green;">what is function</mark>

dev.fun now supports **vibe-coded backend logic**, allowing your app to go beyond frontend interactions.

<mark style="color:green;">**function**</mark> is how you define this backend logic in your dev.fun app. It lets you write reusable functions that can be triggered by user interactions, other functions, or app state changes. Functions enable dynamic, interactive behavior—providing powerful backend capabilities for your apps, all written in natural language.

***

### <mark style="color:green;">how to create a function</mark>

to add a function to your dev.fun app, simply describe to AI prompt

&#x20;\
**Example prompt:**

> “Create a function called rollDice that returns a random number between 1 and 6.”

This will:

* Create a callable function named `rollDice`
* Define its logic based on your prompt
* Allow you to trigger this function from buttons, other functions, or conditionals

***

### <mark style="color:green;">when should I use function</mark>

use <mark style="color:green;">**function**</mark> when your app needs to run key logic in the background, instead of relying on the frontend. This helps improve security, fairness, and reusability across your app.

#### 1. You want to **protect key logic** from user manipulation

Frontend logic can be tampered with—so for critical decisions like who wins a game, score calculation, or payment validation, it's safer to run that logic in a backend function.

> "Create a function called `checkWinner` that compares player scores and decides the winner."

#### 2. You want to **reuse a central logic block**

Functions are reusable. You can call them from multiple places (buttons, other functions, conditionals) and avoid duplicating logic in multiple parts of your app.

> \
> "A `calculateReward` function that’s used both after game win and after daily login."

#### 3. You want to **call an external API securely** using an API key

You can securely store and use secrets (like API keys) inside a function, so they’re never exposed to the user’s browser.

> \
> “Call the OpenAI API using `SECRET_KEY` and return a text response.”

***

### <mark style="color:green;">how to create a function to call API using API secret</mark>

function can securely make an API call with API secret. You can create your API secret under the "secret keys" in the "configuration" tab. and tell AI to use the secret to make API call.&#x20;

&#x20;\
**Example prompt:**

first, create MY\_SECRET\_KEY  in the configuration tab with the API key.&#x20;

<figure><img src="https://3868857016-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fz4tS1GRQLEQOUjZoPKuP%2Fuploads%2FV00Y1CXYKi7pCiJd7EWl%2FScreenshot%202025-07-17%20at%2010.09.54%E2%80%AFAM.png?alt=media&#x26;token=8dd3cd9d-d107-4a07-b78a-4d384aa96c04" alt=""><figcaption></figcaption></figure>

then tell dev.fun AI:

> “Create a function that call API <https://dexscreener.com/solana/> using MY\_SECRET\_KEY. The expected response of the API is \<JSON format API response> . Display the result on the website”

***

### <mark style="color:green;">how to trigger a function</mark>

Functions can be triggered by certain action on your dev.fun app. You can link any function to a button, form, or interaction in your app.

**Example prompt:**

> "when user click roll dice button, trigger rollDice function"

\
If you have a function `checkWinner`, you can create another function that calls it:<br>

> “Create a function that runs `checkWinner` after `rollDice` finishes.”

This allows for chaining logic and building complex interactions step-by-step.

***

### <mark style="color:green;">how to use function inputs and outputs</mark>

Functions can take **inputs** (e.g., a user’s name or a number) and return **outputs** that affect your app.\
You can define these inputs/outputs in the function editor or specify them in your prompt.

Example prompt:

> \
> “Create a function called `greetPlayer` that takes a name and returns `Hello, {name}!`”

You can then:

* Pass values into this function from inputs or other functions
* Display its output in the UI or use it in other logic

***

### <mark style="color:green;">how to view your functions</mark>

All your functions are visible under the **“configuration”** tab.&#x20;

<figure><img src="https://3868857016-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fz4tS1GRQLEQOUjZoPKuP%2Fuploads%2FiuyVLTLOGc75Dop5uaYh%2FScreenshot%202025-07-17%20at%2010.16.28%E2%80%AFAM.png?alt=media&#x26;token=d20191de-1952-49cf-8420-20ff79b8decb" alt=""><figcaption></figcaption></figure>

***

### <mark style="color:green;">how to debug or explain a function</mark>

If you’re not sure what a function does or why it behaves a certain way, switch to **“brainstorm” mode** and ask the dev.fun AI to explain it.

Example:

> “Explain what my `checkWinner` function does.”

The AI will describe:

* What the function is doing
* What inputs it uses
* What it returns or affects in your app
