React
The React SDK is a thin wrapper around the JavaScript SDK. It allows you to add Evervault's UI components, such as the Card component, to any React app.
Getting Started
Our React SDK is distributed via npm and can be installed using your preferred package manager.
Once installed, Initialize the SDK by wrapping your application with the EvervaultProvider component.
Encrypting Data
Once you've added the <EvervaultProvider>, you can access the useEvervault() hook in its children. The useEvervault() hook returns an initialized instance of the JavaScript SDK, which includes the encrypt() function. The encrypt() function can be used to encrypt plaintext data in your application.
Components
EvervaultProvider
Sets up a client for interacting with Evervault. You must provide a teamId and appId to the provider.
Props
The unique identifier for your Team.
The unique identifier for your App.
A callback function that is called if the SDK fails to load. Use the isScriptLoadError utility to determine the cause of the error.
The timeout in milliseconds for the SDK to load. Defaults to
15000(15s).
The EvervaultProvider accepts a ref that exposes a reload() method that you can use to reload the script if it fails.
Ref Methods
Attempts to reload the Evervault script. Typically called in response to an
onLoadErrorevent.
The Card component collects and encrypts card data for Card Collection in a completely PCI-compliant environment. You can build it in two ways.
- Composable: compose the fields yourself as children of
<Card>. They render in the order you write them, so you control the layout. - Config: configure the Card component with props on
<Card>. The fields render in a fixed order.
Declare the Card component's fields as children of <Card>. They render in the order you write them, and Card.Row places fields side by side.
A <Card> with children renders the <ev-card> element from the JavaScript SDK, so it follows the same rules. It mounts with the client from the nearest EvervaultProvider.
Children can change after the component mounts, and the details a customer has already entered are kept.
Card component field components
Each field is declared as its own component. Declare each field once. A duplicate field is ignored and logs a warning.
Components
The cardholder name field.
The card number field.
The expiry field, with the month and year in a single input.
The expiry month field of a split expiry.
The expiry year field of a split expiry.
The CVC field.
A custom field for your own, non-card data.
Places the fields side by side, sharing the width of the component.
Any other child is ignored, and the console logs a warning that names it. A Card component with children ignores the fields prop and logs a warning.
With autoProgress set, focus moves to the next field in the order the fields are declared, including fields inside a Card.Row. Backspace in an empty field moves focus back to the previous one.
Split expiry
The expiry can be declared as a single Card.Expiry or as separate Card.ExpiryMonth and Card.ExpiryYear fields. The month and year can be placed independently of each other, but a warning is logged if other fields are declared between them.
A split expiry still reports a single expiry in the card payload, under card.expiry, and its errors under errors.expiry. When the date is invalid, both fields are marked invalid.
You can't combine the two forms. A Card component that declares a month without a year, a year without a month, or either one alongside Card.Expiry logs an error and isn't rendered. If the component is already mounted, it keeps its previous fields.
Custom fields
Card.Field collects your own, non-card data inside the component, such as a postcode or an email address. It's styled by the same theme as the card fields. Each value is encrypted before it leaves the iframe.
The encrypted values are reported in the fields object of the card payload, by name. A field that's empty or invalid is reported as null, and its error is reported under errors.fields as required or invalid.
Validation follows the same rules as HTML form validation. Errors show after the customer leaves the field, or on every field when you call the validate() ref method. The Card component isn't complete until every custom field is valid.
Changing a field's validation rules clears its value, so a value is only ever validated against the rules it was entered under. This prevents a script on your page from changing a pattern repeatedly to determine what was typed.
A field without a name, or with the same name as another field, isn't rendered and logs a warning. Themes can target the field as [ev-name="field-<name>"].
Card component props
Component-wide settings are props of <Card>. A field's own prop takes precedence over the same setting on <Card>.
Props
The theme to use for the component. See the styling section for more information.
The root color-scheme of the iframe document. For a seamless transparent background, use the same color scheme as the parent document. Only read when the Card component mounts.
Displays an icon for the detected card brand. You can customize icons by passing an object with the brand as the key and the icon URL as the value.
Focuses the Card component when it mounts. A field's own
autoFocusprop takes precedence.Moves focus to the next field after the current one is complete. A field's own
autoProgressprop takes precedence.Enables browser autocomplete for every field. A field's own
autoCompleteprop takes precedence.The card brands to accept. If not set, all brands are accepted. Possible values are
visa,mastercard,american-express,diners-club,discover,jcb,unionpay,maestro,elo,mir,hiper,hipercard,szep,uatp,rupay.Custom card brand definitions created with brands.create. Custom brands are always accepted, even when
acceptedBrandsis set. See the custom card brands section for usage examples.Default values for the component's fields.
Customizes the text shown in the component. A field's own
label,placeholder, anderrorMessageprops take precedence.The label for the name field.
The placeholder for the name field.
The error message for the name field when the value is invalid.
The error message for when the name field fails regex validation.
The label for the number field.
The placeholder for the number field.
The error message for the number field when the value is invalid.
The error message for a card brand that isn't accepted.
The label for the expiry field.
The placeholder for the expiry field.
The error message for an invalid expiry, for both the combined and the split expiry.
The label for the expiry month field.
The placeholder for the expiry month field.
The label for the expiry year field.
The placeholder for the expiry year field.
The label for the CVC field.
The placeholder for the CVC field.
The error message for the CVC field when the value is invalid.
The error message for every custom field that's required but empty. Defaults to
This field is required.The error message for every custom field with an invalid value. Defaults to
Please enter a valid value.The
label,placeholder,errors.required, anderrors.invalidtext of a single custom field, by name. Takes precedence overtranslations.field.
Customizes the validation of the fields.
A regex pattern that the name must match in order to be considered valid. The
patternprop ofCard.Holdertakes precedence.When set to
true, CVCs aren't required. CVCs are still validated if provided. Theoptionalprop ofCard.Cvctakes precedence.Validation rules for a single custom field, by name. Accepts
required,pattern,minLength,maxLength,min,max, andstep, which work like the Card.Field props of the same name. A field's own props take precedence.
Registers WebMCP tools inside the iframe so browser agents can interact with the component. Accepts the same object as the
agentToolsprop of a Card component configured with props. Only read when the component mounts.Hides the component when loaded. Call the show() ref method to show it. Only read when the component mounts.
Called whenever the component's state changes.
The card holder name.
The encrypted card number. This will only be present if the number is valid.
The brand that issued the card.
The last 4 digits of the card. This will only be present if the number is valid.
The BIN for the card.
The month for the card expiry field.
The year for the card expiry field.
The encrypted card CVC.
Validation errors related to the card details.
Whether or not there are any errors shown in the component.
Whether or not all of the fields have been filled out successfully with valid values.
Called after every field, including custom fields, holds a valid value, and again on each change while every field stays valid. Receives the same payload as
onChange.Called when the fields are validated with the validate() ref method. Receives the same payload as
onChange.Called when a card reader is used. The component must have focus for the card reader to be detected. The payload has the encrypted
number, thebrand,lastFour,bin, andexpirywith itsmonthandyear, and the cardholder'sfirstNameandlastNameif detected.Called when a field gains focus.
fieldisname,number,expiry,cvc, orfieldfor a custom field, whose name is inname. Both fields of a split expiry reportexpiry.Called when a field loses focus. Receives the same event as
onFocus.Called on a key-down event within a field. Receives the same event as
onFocus.Called on a key-up event within a field. Receives the same event as
onFocus.Called after the component loads and is ready to be used.
Called when the component fails to load, including when the Evervault client fails to load.
The card payload also includes fields, the encrypted values of the custom fields by name. Errors for custom fields are under errors.fields, by name, as required or invalid.
The redactCVC and allow3DigitAmexCVC props, and an autoComplete object by field, are deprecated on a Card component with children. They still apply to fields that don't set their own redact, allow3DigitAmex, or autoComplete prop.
Card component field props
Each field component takes its own settings as props. A field's own prop takes precedence over the same setting on <Card>.
Props
The text of the field's label.
The field's placeholder.
Text shown beside the label. Themes can target it as
[ev-tooltip].Focuses the field when the component renders. If more than one field sets it, the first is focused. Focus doesn't move after the customer starts entering details.
Moves focus to the next field after this one is complete. Set it to
falseto turn auto-progress off for this field only. The cardholder name doesn't auto-progress. The CVC advances after the max number of digits are entered (the max depends on the card brand).Enables browser autocomplete for the field.
Replaces the text of the field's error. On a split expiry, either the month or the year can set it. On the card number field, the error for a card brand that isn't accepted is set separately, with
unsupportedBrandMessage.Card.Holderonly. Fills in the cardholder name. The value only applies while the field is untouched, and never replaces a name the customer has typed.Card.Holderonly. A regular expression the whole cardholder name must match.Card.Numberonly. Added to the field as theev-icon-positionattribute, so your theme can position the brand icon.Card.Numberonly. Replaces the error text for a card brand that isn't accepted.Card.Cvconly. Masks the CVC as it's typed.Card.Cvconly. Makes the CVC optional. CVCs are still validated if provided.Card.Cvconly. Set it tofalseto require a 4-digit CVC for American Express cards. Accepted by default.
Custom field props
Card.Field takes label, placeholder, tooltip, autoFocus, autoProgress, errorMessage, and defaultValue, as well as the following props.
Props
The field's name. Its value is reported under this key in the
fieldsobject of the card payload.The input type. One of
text,email,tel,url,number, ordate. Defaults totext.Rejects an empty value.
A regular expression the whole value must match. Doesn't apply to
numberordatefields.The minimum length of the value. Doesn't apply to
numberordatefields.The maximum length of the value. With
autoProgressset, focus moves to the next field after the value reaches this length.The minimum value of a
numberordatefield.The maximum value of a
numberordatefield.The step of a
numberordatefield, counted frommin. Defaults to1, measured in days for adatefield. Useanyto accept any value.Prevents the customer from editing the value. A read-only field isn't validated.
An autocomplete token, such as
postal-code, ortrueorfalseto turn autofill on or off.One of
characters,words,sentences, ornone. Capitalizes the value as it's typed, socharacterslets a lowercase value match an uppercasepattern. Only applies totextandtelfields.The inputmode.
Enables the browser's spell checking.
The enterkeyhint.
An email or url field must also hold a valid email address or URL. An empty required field shows This field is required, and an invalid value shows Please enter a valid value, unless the field sets an errorMessage. You can also change this text with the translations prop.
Ref methods
The Card component accepts a ref that exposes the following methods.
ThreeDSecure
The ThreeDSecure component can be used instead of the useThreeDSecure hook when you want more control over how and where the 3D Secure iframe is displayed. In order to use the component you must first create a 3D Secure session on your backend.
Props
The 3D Secure session ID. A 3D Secure session can be created using the API.
Allows you to customize the appearance of the component. Note: You can't customize the appearance of the iframe content itself. This is controlled by the card issuer.
Allows you to set the root color-scheme for the iframe document. For a seamless transparent background, use the same color scheme as the parent document.
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.
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.
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.
The error event will be fired if the component fails to load.
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.
Reveal
The Reveal component allows you to display previously encrypted card data to your users in plaintext in a secure iframe hosted by Evervault. See Card Reveal for more information.
Props
The request to use to fetch the encrypted data.
Triggered when the component has fully loaded and is ready to be shown.
Triggered when the component fails to load.
Reveal.Text
Creates a Reveal Text consumer component. The Reveal Text consumer allows you to render a selected field from the request response. This component must be rendered as a child of the Reveal component.
Props
Allows you to completely customize the appearance of the component.
Allows you to set the root color-scheme for the iframe document. For a seamless transparent background, use the same color scheme as the parent document.
A JSON path selector for the response field you want to display.
Reveal.CopyButton
Creates a Reveal Copy Button consumer component. This renders a button which when clicked will copy a response field to the users clipboard. This component must be rendered as a child of the Reveal component.
Pin
Creates a Pin component which allows you to collect and encrypt pin numbers in a completely PCI-compliant environment.
Props
The theme to use for the Pin.
Allows you to set the root color-scheme for the iframe document. For a seamless transparent background, use the same color scheme as the parent document.
If set to true, the component will automatically steal focus when it mounts.
Change the length of the pin number.
If set to 'alphanumeric' the pin number will also accept letters as input.
Sets the
typeattribute of the underlying input elements used to capture the pin. Defaults to"number".Triggered when the component has fully loaded and is ready to be shown.
Triggered when the component fails to load. If you want to respond to validation errors you should use the change event instead.
Hooks
useEvervault
The useEvervault hook is accessible in children of the EvervaultProvider, and returns an initialized instance of the Evervault JavaScript SDK. One of the functions included in the returned object is encrypt(), which can be passed any plaintext data structure.
.encrypt()
Encrypts data using Evervault Encryption. Evervault Strings can be used across all of our products. It is accessible on the returned value from the useEvervault() hook. To encrypt data using the React.js SDK, simply pass a String or an Object into the evervault.encrypt() function.
The encrypted data can be passed to your server and stored in your database as normal. It can also be used with any of Evervault's other services.
.decrypt()
Allows you to decrypt a previously encrypted piece of data using a client side token. The token is a time bound token for decrypting data. 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.
The payload can be any String or Object and it will be returned, decrypted, in the same form.
Parameters
A valid client-side token with permissions to decrypt the data.
The encrypted data to decrypt.
.brands.create()
Creates a custom card brand definition that can be passed to the customBrands prop on Card. Use this to add support for card networks that aren't included in Evervault's built-in brand list.
Parameters
A unique identifier for the brand. This value is returned in the
localBrandsarray of the card change payload.Configuration for the custom brand.
Rules used to validate card numbers for this brand.
Bank identification number (BIN) prefixes or ranges that identify cards belonging to this brand. A single number matches cards starting with that prefix (e.g.,
9900). A two-element tuple defines an inclusive range of BIN prefixes (e.g.,[88000, 88999]).The valid card number lengths for this brand.
Whether to validate the card number using the Luhn algorithm. Defaults to
true.
A URL for the brand's icon. Displayed in the card component when a matching card number is entered.
useThreeDSecure
The useThreeDSecure hook can be used in combination with the Evervault API to perform 3D Secure authentication. In order to use the hook you must first create a 3D Secure session on your backend and pass it to your frontend. The hook will manage displaying the 3D Secure iframe inside of a modal window and handle the authentication process.
Note: If you want more control over how and where the 3D Secure iframe is displayed you can use the ThreeDSecure component instead.
See 3D Secure for more information.
Options
Allows you to customize the appearance of the component. Note: You can't customize the appearance of the iframe content itself. This is controlled by the card issuer.
Allows you to set the root color-scheme for the iframe document. For a seamless transparent background, use the same color scheme as the parent document.
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.start()
The start function is used to begin the 3D Secure authentication process.
Parameters
The 3D Secure session ID. A 3D Secure session can be created using the API.
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.
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.
The error event will be fired if the component fails to load.
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.update()
The update function updates the options passed to the active 3D Secure instance without restarting the authentication flow.
Parameters
Allows you to customize the appearance of the component. Note: You can't customize the appearance of the iframe content itself. This is controlled by the card issuer.
Allows you to set the root color-scheme for the iframe document. For a seamless transparent background, use the same color scheme as the parent document.
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.
Utilities
isScriptLoadError
The isScriptLoadError function can be used to determine if an error was caused by the Evervault.js browser script failing to load.
Known Error Codes
The window object isn't available.
The document's head or body element wasn't found to mount the Evervault script tag.
The <script> tag encountered an error when loading the Evervault script.
The script failed to load before the specified loadTimeout period.
The Evervault object wasn't available after the script loaded.
When using the AMD module loader, the Evervault module wasn't available after the script loaded.
The AMD module loader encountered an error when loading the Evervault module.
Content Security Policy (CSP)
If you are using a Content Security Policy (CSP), you will need to add the following directives to allow the Evervault SDK to function correctly.