JavaScript
Installation
Our JavaScript SDK is distributed from our CDN and can be loaded with a simple script tag. The SDK must be loaded directly from our CDN and cannot be bundled with your application or self hosted.
Using npm
You can also install Evervault via the @evervault/js package on npm. This package is a light wrapper which handles loading the SDK from our CDN and also provides TypeScript definitions.
Note: It is important to note that for PCI Compliance you must load the Evervault SDK directly from our CDN and cannot bundle it with your application. The loadEvervault function will always load the latest version of the Evervault SDK regardless of which version of @evervault/js you are using.
Asynchronous Loading
You can load the SDK asynchronously using the async attribute on the script tag to prevent blocking the loading of your page. However, it is important to note that you will need to wait for the SDK to load before making any API calls.
Usage
Once loaded, you can initialize the Evervault SDK with your Team and App ID found in the Evervault Dashboard.
Reference
Evervault
Creates an instance of the Evervault SDK using your Team and App ID.
Parameters
The ID of your Evervault team. This can be found inside of your team settings on the Evervault dashboard.
The ID of your Evervault app. This can be found inside of your app settings on the Evervault dashboard.
encrypt
Encrypts data using Evervault Encryption. The encrypted data can be stored in your database or file storage as normal. Evervault Strings can be used across all of our products. Evervault File Encryption is currently in Beta, and files can only be decrypted with Relay.
decrypt
Decrypts data previously encrypted with an Evervault product or SDK.
The decrypt() function uses Client-Side Tokens. The token can be generated using our backend SDKs or through our REST API.
The payload must be the same payload that was used to create the token and expires in a maximum of 10 minutes depending on the expiry set when creating the token.
Parameters
A Client-Side Token with permission to decrypt the given payload.
The encrypted data to decrypt.
ui.card
Initializes a Card component for Card Collection. The Card component makes it easy to collect and encrypt card data in a completely PCI-compliant environment.
Parameters
Allows you to completely customize the appearance of the component. See Custom Styling for more information.
If set to true, the component will display icons for the detected card brand. You can customize icons by passing an object with the brand as the key and src URL as the value.
If set to true, the component will automatically focus when it mounts.
When set to a value in the supported cards list, the component will be restricted to those brands.
Allows you to configure the fields displayed in the component. Possible values are 'name', 'number', 'expiry', and 'cvc'. By default only 'number', 'expiry' and 'cvc' will be shown.
Sets the
typeattribute of the CVC field topasswordin order to visually redact the CVC field..Allows you to customize the text shown inside of the component.
Allows you to customize validation logic for the component.
card.mount
Mounts the component to a given DOM node.
card.on('change')
Subscribe to changes made to the component. The encrypted card details can be accessed from the payload passed to the callback. The change event will fire any time there is a state update in the component. This includes when the user types in a field, looses focus on a field, or when an error is displayed.
Callback Parameters
Information about the entered card, including meta data as well as the encrypted number aned CVC.
Validation errors related to the card details.
Whether or not there are any errors shown. Validation occurs as the user looses focus on a field. You can force validation to occur with the .validate method.
Note: This field represents if there are any errors shown, not if the card details are valid. You should use the isComplete field to determine if the user has successfully entered a valid card.
Whether or not the card details are valid. This will only be true if the user has entered a valid card number, expiry date and CVC.
card.on('complete')
The complete event will fire once the user has successfully filled out all fields in the Card Component with valid values.
Callback Parameters
Information about the entered card, including meta data as well as the encrypted number aned CVC.
Validation errors related to the card details.
Whether or not there are any errors shown. Validation occurs as the user looses focus on a field. You can force validation to occur with the .validate method.
Note: This field represents if there are any errors shown, not if the card details are valid. You should use the isComplete field to determine if the user has successfully entered a valid card.
Whether or not the card details are valid. This will only be true if the user has entered a valid card number, expiry date and CVC.
card.on('ready')
The ready event will be fired once the component has fully loaded and is ready to be displayed. This is often used to show a loading state while the component loads.
card.on('error')
The error event will be fired if the component fails to load. If you want to respond to validation errors you should use the change event instead.
card.on('swipe')
Subscribe to swipe events from card readers. The encrypted card details can be accessed from the payload passed to the callback. The component must have focus for the card reader event to be detected.
Callback Parameters
Information about the entered card, including meta data as well as the encrypted number aned CVC.
card.update()
Update the configuration for the component after it has been initialized. Any arguments passed will be merged with the arguments passed when the component was initialized. This can be used to dynamically update the translations for the component, or update the styling.
Parameters
Allows you to completely customize the appearance of the component. See Custom Styling for more information.
If set to true, the component will display icons for the detected card brand. You can customize icons by passing an object with the brand as the key and src URL as the value.
If set to true, the component will automatically focus when it mounts.
When set to a value in the supported cards list, the component will be restricted to those brands.
Allows you to configure the fields displayed in the component. Possible values are 'name', 'number', 'expiry', and 'cvc'. By default only 'number', 'expiry' and 'cvc' will be shown.
Allows you to customize the text shown inside of the component.
Allows you to customize validation logic for the component.
card.validate()
Manually trigger validation for the card details fields. This method is useful if you want to force validation to display error messages inside of the component. This is an asynchronous operation and will not return an immediate result. You can use the validate event to listen for the result of calling this method.
card.unmount()
Removes the component from the DOM.
ui.threeDSecure
The ThreeDSecure component can be used in combination with the Evervault API to perform 3D Secure authentication. In order to use the ThreeDSecure component you must first create a 3D Secure session on your backend and pass it to your frontend. The component will display the 3D Secure iframe and handle the authentication process.
Parameters
The 3D Secure session ID. A 3D Secure session can be created using the Create 3D Secure Session API endpoint.
Allows you to customize the appearance of the component. See Custom Styling for more information.
Note: You can't customize the appearance of the iframe content itself. This is controlled by the card issuer.
This size of the 3D Secure iframe. Card issuers are required to support content at 250x400, 390x400, 500x600, 600x400. The default size is 500x600.
If set to true, the component will fail 3DS authentication when a challenge is requested and no challenge will be shown. Alternatively, you can provide a function which will be called when a challenge is requested. If the function returns true 3DS authentication will fail, if it returns false the challenge will be shown.
threeDSecure.mount()
Mounts the component to the DOM. If no selector is provided the component will be rendered as a modal window.
Parameters
The selector of the DOM node to mount the component to. If no selector is provided the component will be rendered as a modal window.
threeDSecure.on('success')
The 'success' event will be fired once the 3D Secure authentication process has been completed successfully. You should use this event to trigger your backend to finalize the payment. Your backend can use the Retrieve 3DS Session endpoint to retrieve the cryptogram for the session and complete the payment.
Note: The component is automatically unmounted after the 'success' event is fired.
threeDSecure.on('failure')
The 'failure' event will be fired if the 3D Secure authentication process fails. You should use this event to handle the failure and inform the user and prompt them to try again.
Note: The component is automatically unmounted after the 'failure' event is fired.
threeDSecure.on('ready')
The ready event will be fired once the component has fully loaded and is ready to be displayed. This is often used to show a loading state while the component loads.
threeDSecure.on('error')
The error event will be fired if the component fails to load.
Callback Parameters
A unique code representing the error that occurred.
A human readable message describing the error.
threeDSecure.update()
Update the configuration for the component after it has been initialized. Any arguments passed will be merged with the arguments passed when the component was initialized.
Parameters
Allows you to customize the appearance of the component. See Custom Styling for more information.
Note: You can't customize the appearance of the iframe content itself. This is controlled by the card issuer.
This size of the 3D Secure iframe. Card issuers are required to support content at 250x400, 390x400, 500x600, 600x400. The default size is 500x600.
If set to true, the component will fail 3DS authentication when a challenge is requested and no challenge will be shown. Alternatively, you can provide a function which will be called when a challenge is requested. If the function returns true 3DS authentication will fail, if it returns false the challenge will be shown.
threeDSecure.unmount()
Removes the component from the DOM.
ui.reveal
The Reveal component allows you to display previously encrypted card data to your users in plaintext in a secure iframe hosted by Evervault. Before using Reveal you'll first need to have a JSON endpoint to retrieve data from. This is often your own API endpoint combined with Relay to decrypt previously encrypted data from your database or a third-party API.
It is important that the endpoint that you create sets the applicable CORS headers so that it can be accessed from the Reveal iframe. Otherwise your requests will fail!
reveal.on("ready")
The ready event will be fired once the request has been completed and all reveal consumer components are ready to be shown.
reveal.on("error")
The error event will be fired if the Reveal Component fails to load or the passed request fails.
reveal.text()
Creates a Reveal Text consumer. The Reveal Text consumer allows you to render a selected field from the request response.
Parameters
The JSON path to the field you want to display.
Allows you to completely customize the appearance of the component. See Custom Styling for more information.
Allows you to use regex matching to format the field value.
text.mount()
Mounts the Reveal Text component to a given DOM node.
text.unmount()
Removes the Text Component from the DOM.
reveal.copyButton()
Creates a Reveal Copy Button consumer. This renders a button which when clicked will copy a response field to the users clipboard.
copyButton.on("copy")
The copy event will be fired when the button is clicked and the value has been copied to the clipboard.
copyButton.mount()
Mounts the Reveal Copy Button component to a given DOM node.
copyButton.unmount()
Removes the Copy Button Component from the DOM.
transactions.create
Creates a transaction object that can be used with Apple Pay and Google Pay components.
Parameters
The transaction amount in smallest currency unit (e.g., cents).
The three-letter ISO currency code.
The two-letter ISO country code.
Your Evervault merchant ID.
Specify the type of transaction. Disbursement requests are only available with Apple Pay. Default:
payment.Optional array of line items to display in the payment sheet.
Optional array of recipient details required for collection on a disbursement transactions. Possible values:
name,email,phone,address.
ui.applePay
Initializes an Apple Pay button that handles the payment flow and returns encrypted payment details.
Parameters
The transaction object created with
transactions.create.Configuration options for the Apple Pay button.
applePay.on("success")
Fired when the payment has been successfully authorized.
applePay.on("error")
Fired if there's an error initializing the Apple Pay button.
applePay.on("cancel")
Fired when the user cancels the Apple Pay payment flow.
ui.googlePay
Initializes a Google Pay button that handles the payment flow and returns encrypted payment details.
Parameters
The transaction object created with
transactions.create.Configuration options for the Google Pay button.
googlePay.on("success")
Fired when the payment has been successfully authorized.
googlePay.on("error")
Fired if there's an error initializing the Google Pay button.
googlePay.on("cancel")
Fired when the user cancels the Google Pay payment flow.
googlePay.on("ready")
Fired when the Google Pay button is ready to be displayed.
Custom Styling
Some components in the SDK allow you to customize the appearance of the component using a theme object. A theme is just an object with a styles property. This styles property uses a CSS-as-JS format to define CSS rules for the component. These rules will be compiled to CSS and injected into the iframe.
Although you can customize the CSS inside of the iframe, you cannot modify the HTML. We know that layout can have a big impact on style definitions and so to help with this, we apply custom attributes to various elements within the iframe. These attributes are prefixed with ev-.
You can see our premade Card Collection themes for an example of how these attributes can be used when creating themes.
Responsive Styling
You can define media queries inside of the themes styles object, however, this may lead to unexpected behaviour as the media queries will be matched against the iframe document, not the parent document. To get around this, we provide a media utility which allows you to define styles based on media queries that match the parent document.
To access the media utility you will need to define your theme as a function that returns a theme object. This function will be passed a utilities object as an argument, which contains the media utility. The media utility should be spread into the styles object of the returned theme.