Working with option lists in Xano just got easier! In this guide, we'll show you how to leverage the metadata API to dynamically populate available options on your front-end, eliminating the need for hard-coding or maintaining separate databases. Let's dive in!
An option list in Xano can be either:
These options are typically populated on the front-end through drop-down menus, radio buttons, or other UI components.
To get started, you'll need to generate an access token for the metadata API. This token is required to make API calls and interact with your Xano instance programmatically.
Next, you'll need to locate the API endpoints you'll be working with. In the "Instance API" section, click the link to open the metadata API documentation in a new tab.
Let's start by exploring how to work with separate option fields in your database.
Identify the Option Fields: In your database table, ensure that the fields representing options are tagged with a specific identifier (e.g., "option_"). This helps distinguish them from other fields.
Get the Table Schema: In the metadata API documentation, locate the "Get Table Schema" endpoint. Click the lock button and paste your access token to authenticate your API calls.
Retrieve Table and Workspace IDs: Obtain the workspace ID from the URL of any page in your Xano instance. For the table ID, you'll need to know the ID of the table containing your option fields.
Execute the API Call: In the "Get Table Schema" endpoint, enter the workspace ID and table ID, then click "Execute." This will return the schema for your table, including the option fields.
Copy the cURL Command: Copy the cURL command from the response, as you'll need it to import the API request into Xano.
Create a New API Endpoint: In Xano, create a new API endpoint and import the cURL command you copied earlier.
Extract the Option Fields: Use the "Find All Elements" function to extract the option fields from the array of columns returned by the API call.
Format the Options: Depending on your front-end requirements, you may need to format the option values into a specific array or data structure. The guide provides an example of creating a simple text array.
Update the Response: Update the API response to include the formatted option values, making them available for your front-end to consume.
If you're using an enum field for your options, the process is slightly different.
Get the Table Schema: Follow the same steps as before to retrieve the table schema, but this time for the table containing your enum field.
Copy the cURL Command: Copy the cURL command from the response.
Create a New API Endpoint: In Xano, create a new API endpoint and import the cURL command.
Return the Enum Values: The API response will already include the enum values in the desired format. Simply update the response to return the appropriate array of values.
By following these steps, you'll be able to dynamically populate option lists on your front-end, eliminating the need for hard-coding or maintaining separate databases. This approach ensures that your options are always up-to-date and consistent across your application.
Remember, Xano is here to simplify the process of backend development, making it accessible to everyone, from no-code enthusiasts to experienced developers. Happy coding (or no-coding)!
Join 100,000+ people already building with Xano.
Start today and scale to millions.
Start building for free