Velo by Wix is an innovative product that lets you build robust web applications with zero setup. Work in Wix's visual builder, add custom functionality and interactions using Velo APIs, and enjoy serverless coding in both the front-end and backend. With Velo, your web app can be up and running in a fraction of the time it would normally take you.
To introduce you to Velo, we created our own version of a "Hello, World!" example: a simple currency converter site that uses the wix-fetch API to connect to a 3rd-party service. Site visitors choose source and target currencies from dropdowns and enter an amount to convert. The results are displayed in a text box.
Follow the steps below to get familiar with the basic structure and syntax of Velo.
Click Dev Mode in your site's top bar and turn on Enable Developer Mode in the dropdown.
Add page elements in the Wix Editor:
Element | Location in Add Menu | Description | ID |
---|---|---|---|
Dropdown | User Input | For selecting the source currency | sourceCurrency |
Dropdown | User Input | For selecting the target currency | targetCurrency |
Input | User Input | For entering the amount to convert | sourceAmount |
Text Box | User Input | To display the converted amount | targetAmount |
Button | Button | To trigger the currency conversion when clicked | calculateButton |
Notes
- All the code for this example is added to a single page on the site. In this section we divided the code into short blocks followed by explanations. To see the complete code for this example without comments, scroll down to the end of the tutorial.
- See our API Reference to learn more about the Velo-based code in this example.
To add the code:
Double-click Home Page Code at the bottom of the Editor to open the Code Panel.
Add the following code to the top of the code in the tab before the onReady function:
Copy Code
// The getJSON function in wix-fetch lets you retrieve a// JSON resource from the network using HTTPS.import {getJSON} from 'wix-fetch';// Set the URL of the 3rd-party service.const url = "https://api.exchangerate.host/convert";// Define the currency option values and text for the dropdowns.let currencyOptions = [{ "value": "USD", "label": "US Dollars"},{ "value": "EUR", "label": "Euros"},{ "value": "JPY", "label": "Japanese Yen"},];
Add the code below to the onReady function. Code inside the onReady function runs when the page loads.
Copy Code
$w.onReady(function () {// Set the currency options for the dropdowns.populateDropdowns();// Set the onClick event handler for calculateButton to calculate the target amount.$w('#calculateButton').onClick((event) => {calculateCurrency();})});
The $w
function can select elements on a page by ID or by type, allowing us to run functions and define the properties of the elements. Use this syntax to select an element by ID, $w("#myElementId")
, and this syntax to select by type, $w("ElementType")
.
Here we select the button and define an onClick
event handler to calculate the target amount.
4. Add code to define the functions:
Copy Code
**populateDropdowns( )**```javascript// Populate the dropdowns.function populateDropdowns(){//Set the dropdown options.$w("Dropdown").options = currencyOptions;// Set the first dropdown option as the initial option.$w("Dropdown").selectedIndex = 0;}```Here we select all the dropdowns by type. By calling [`$w`](https://www.wix.com/velo/reference/$w.html) with the element type "Dropdown", we select all dropdowns on the page.**calculateCurrency( )**```javascript// Calculate the target amount.function calculateCurrency() {// Initial amountlet initialAmount = $w("#sourceAmount").value;// Original currencylet sourceSymbol = $w("#sourceCurrency").value;// Target currencylet targetSymbol = $w("#targetCurrency").value;// Define the full url.let fullUrl = `${url}?from=${sourceSymbol}&to=${targetSymbol}`;// Call the wix-fetch API function to retrieve the JSON resource.getJSON(fullUrl).then(json => {// Set the target amount as the initial amount multiplied by// the conversion rate.$w("#targetAmount").value = initialAmount * json.info.rate;})}```We use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to define the full URL, which includes the source and target currencies.The wix-fetch API [`getJSON`](https://www.wix.com/velo/reference/wix-fetch.html#getJSON) function retrieves the JSON resource using the full URL. `getJSON` returns a [promise](/articles/coding-with-velo/java-script-velo/working-with-promises), which resolves to a JSON object.We multiply the retrieved rate by the initial amount and assign it to the `targetAmount` text box.
Now it's time to test your site:
That's it! In just a few minutes, you created a web application in Velo! No setup, no managing server infrastructure, just integrating Velo APIs with the Wix visual builder.
Now that you've had a taste of Velo, check out what else you can do:
Here is the complete code for this example, without comments:
Copy Code
import {getJSON} from 'wix-fetch';const url = "https://api.exchangerate.host/convert";let currencyOptions = [{ "value": "USD", "label": "US Dollars"},{ "value": "EUR", "label": "Euros"},{ "value": "JPY", "label": "Japanese Yen"},];$w.onReady(function () {populateDropdowns();$w('#calculateButton').onClick((event) => {calculateCurrency();})});function populateDropdowns(){$w('Dropdown').options = currencyOptions;$w('Dropdown').selectedIndex = 0;}function calculateCurrency() {let initialAmount = $w("#sourceAmount").value;let sourceSymbol = $w("#sourceCurrency").value;let targetSymbol = $w("#targetCurrency").value;let fullUrl = `${url}?from=${sourceSymbol}&to=${targetSymbol}`;getJSON(fullUrl).then(json => {$w("#targetAmount").value = initialAmount * json.info.rate;})}