# Functions

## Overview

Functions are utility operations that Chargebee.js runs on behalf of your site during a checkout or sign-up flow. They let you carry out an activity or fetch information about a customer before you submit anything to Chargebee. For example, you can check whether an EU VAT number is valid before you exempt a customer from taxes.

Functions ship as a separate module. Load the module once with [`load('functions')`](#load-functions), then call the function you need on the [`chargebee` object](https://www.chargebee.com/docs/js/cbinstanceobj-api-ref).

## Prerequisites

Before you use functions, ensure that you have:

- [Set up Chargebee.js](https://www.chargebee.com/docs/js/settingup-api-ref) in your application.
- Initialized Chargebee.js with a [publishable API key](https://www.chargebee.com/docs/billing/2.0/site-configuration/api_keys#publishable-key). Functions call Chargebee through the JavaScript API, which authenticates with the publishable key.

## `load('functions')`

Loads and initializes the functions module, and attaches the function namespaces to the `chargebee` object.

### Syntax

```js
chargebee.load('functions')
```

### Example

```js
const chargebee = Chargebee.getInstance();

chargebee.load('functions').then(() => {
  // The functions module is ready. Call chargebee.vat.validateVat() here.
});
```

### Return value

Returns a promise that resolves when the module has loaded. Call functions only after the promise resolves.

## EU VAT validation

Chargebee validates a VAT number by sending a validation request to the [VAT Information Exchange System (VIES)](http://ec.europa.eu/taxation_customs/vies/), a search service owned by the European Commission.

> **Prerequisite**
>
> To carry out EU VAT validation, you must set your [organization address](https://www.chargebee.com/docs/billing/2.0/taxes/tax#prerequisites) in Chargebee Billing.

### `vat.validateVat(options)`

Validates a European Union VAT number against VIES.

#### Syntax

```js
chargebee.vat.validateVat(options)
```

#### Example

```js
const chargebee = Chargebee.getInstance();

chargebee.load('functions').then(() => {
  chargebee.vat.validateVat({
    country: "DE",
    vat_number: "DE123456789"
  }).then(result => {
    // Handle the validation result.
  }).catch(error => {
    // Handle the error.
  });
});
```

#### Parameters

- `country` (String, required) 2-letter ISO 3166 alpha-2 country code
- `vat_number` (String, required) EU VAT Number

#### Return value

Returns a promise that resolves to an object holding the validation status and, when VIES returns one, a message. The promise rejects if either `country` or `vat_number` is missing.

##### Sample response

```json
{
  "status": "INVALID",
  "message": "Customer VAT number is invalid"
}
```

##### Response properties

- `status`: The validation result.

<Table>
  <TableHeader>
    <TableRow>
      <TableHead>

Status

      </TableHead>
      <TableHead>

Description

      </TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
      <TableRow>
        <TableCell>

`VALID`

        </TableCell>
        <TableCell>

VAT number validation was successful and the number is valid.

        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell>

`INVALID`

        </TableCell>
        <TableCell>

VAT number validation failed.

        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell>

`UNDETERMINED`

        </TableCell>
        <TableCell>

No response from VIES, or another error occurred.

        </TableCell>
      </TableRow>
  </TableBody>
</Table>

- `message`: Describes the response received from VIES. This property is present only when VIES returns comments.
