Data Management

How to pass data in the function stack using variables

Hey there! In this guide, we'll explore how to use variables in the Xano function stack. Variables are essential for storing and referencing information, making your development process more efficient and organized.

Understanding Variables

Variables are like containers that hold and store information, allowing you to reference and use that information in other functions. The majority of functions in Xano (around 90%) have some kind of output in the form of a variable that you can utilize elsewhere.

Retrieving Data with Variables

Let's start with a simple example. Suppose we want to retrieve a user record from the database:

  1. Add a Database Request function to your API endpoint.
  2. Configure the function to fetch a record from the user table with a specific ID (e.g., 1).
  3. Observe the output: Xano will return the user record as a variable (e.g., user_1).
  4. Rename the variable (optional): Go to the output section and rename the variable to something more descriptive, like user.
  5. Test the endpoint: Run the API, and you'll see the user information returned in the response.

Now that the user data is stored in the user variable, you can reference and manipulate it in other functions.

Creating New Variables

Sometimes, you might want to create a new variable based on an existing one. For example, let's extract the user's name from the user variable:

  1. Add a "Create Variable" function.
  2. Name the new variable (e.g., myName).
  3. Reference the existing variable: Use dot notation (e.g., user.name) to access the desired property from the user variable.
  4. Test the new variable: Add a response function and return myName to see the user's name in the API response.

Updating Variables

You can also update existing variables by applying filters or manipulations. For instance, let's convert the user's name to uppercase:

  1. Add an "Update Variable" function.
  2. Select the variable to update (e.g., myName).
  3. Apply a filter: Use the uppercase filter to convert the name to uppercase.
  4. Test the updated variable: Return myName in the response to see the uppercase name.

Remember, a variable must be established earlier in the function stack for you to reference it in subsequent functions.

Using Variables in Database Operations

Variables can also store information to be added or updated in your database. Let's create a new user record using the existing user variable:

  1. Add a "Database Request" function to insert a record into the user table.
  2. Map the input fields: Use dot notation (e.g., user.name, user.email) or the magic wand tool to map the variable properties to the input fields.
  3. Configure default values (optional): Check the "Default Values" box to apply the entire variable as default values for the input fields.
  4. Test the operation: Execute the API endpoint to create the new user record based on the existing user variable.

Conclusion

Using variables in the Xano function stack is crucial for efficient data handling and manipulation. By understanding how to create, update, and reference variables, you can streamline your development process and build more complex applications with ease.

Keep in mind that this guide covers the basics of working with variables in Xano. As you gain more experience, you'll discover advanced techniques and use cases for leveraging variables in your projects.

Happy coding with Xano!

Sign up for Xano

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free