# Wix Code

Wix Code allows writing custom Javascript code as part of websites built with Wix.com. In this example we are going to show how to use EmailJS REST API to send emails from Wix Code.

The following example will show how to send an email after a button was clicked, but the same logic can be applied to various events of almost any element on the page.

To learn the basics of Wix Code and how to add interactivity to an editor element please refer to this Wix Code article.

// For full API documentation, including code examples,
// visit http://wix.to/94BuAAs
import {fetch} from 'wix-fetch';
 
$w.onReady(function () {
  //TODO: write your page related code here...
});
 
export function button1_click(event, $w) {
  // Change all values to your own
  let params = {
      user_id: 'YOUR_USER_ID',
      service_id: 'YOUR_SERVICE_ID',
      template_id: 'YOUR_TEMPLATE_ID',
      template_params: {
        'YOUR_PARAM1_NAME': 'YOUR_PARAM1_VALUE',
        'YOUR_PARAM2_NAME': 'YOUR_PARAM2_VALUE'
      }
  };

  let headers = {
      'Content-type': 'application/json'
  };

  let options = {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(params)
  };

  fetch('https://api.emailjs.com/api/v1.0/email/send', options)
    .then((httpResponse) => {
        if (httpResponse.ok) {
            console.log('Your mail is sent!');
        } else {
            return httpResponse.text()
              .then(text => Promise.reject(text));
        }
    })
    .catch((error) => {
        console.log('Oops... ' + error);
    });
}