# Wix Code

Wix Code (opens new window) allows writing custom Javascript code as part of websites built with Wix.com (opens new window). 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 (opens new window).

// 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
  const params = {
    user_id: 'YOUR_PUBLIC_KEY',
    service_id: 'YOUR_SERVICE_ID',
    template_id: 'YOUR_TEMPLATE_ID',
    template_params: {
      YOUR_PARAM1_NAME: 'YOUR_PARAM1_VALUE',
      YOUR_PARAM2_NAME: 'YOUR_PARAM2_VALUE',
    },
  };

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

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

  fetch('https://api.emailjs.com/api/v1.0/email/send', options)
    .then((response) => {
      if (response.ok) {
        console.log('SUCCESS!');
      } else {
        return response.text().then((text) => Promise.reject(text));
      }
    })
    .catch((error) => {
      console.log('FAILED... ', error);
    });
}