Set up the JavaScript SDK
This page explains how to load the Braintree JavaScript SDK and install it with npm or AMD, so you can build a Hosted Fields integration.
Braintree's JavaScript SDK lets you accept payments while maintaining PCI compliance.
The SDK lets you accept payments from credit cards, PayPal, and other payment methods. Each of these payment methods is its own component, so you only need to pull in the files you need.
Using a custom integration, you can take advantage of Hosted Fields to preserve your user experience while maintaining SAQ A compliance.
Prerequisites
Before you load the SDK, get a CLIENT-AUTHORIZATION value from your server. See
Client Authorization for how to generate a tokenization key or client token.
Load the SDK
The JavaScript SDK is split into a number of components. You can load these components directly from our servers:
- HTML
<!-- Load the required client component. -->
<script src="https://js.braintreegateway.com/web/3.146.0/js/client.min.js"></script>
<!-- Load additional components when required. -->
<!-- Use the components. We'll see usage instructions next. -->
<script>
braintree.client.create(/* ... */);
</script>We recommend using the latest version of each SDK component.
Our JS SDK and its components follow semantic versioning. When updating your integration, update each component to the latest version.
Install using other methods
We also publish these packages in a few other ways.
npm
We ship an npm package to be used with tools like Browserify or Webpack. Install it as you would any other npm package:
- Bash
npm install --save braintree-webYou can then include each component as needed.
- JavaScript
const client = require('braintree-web/client');
const hostedFields = require('braintree-web/hosted-fields');
async function setup() {
try {
const clientInstance = await client.create({
authorization: 'CLIENT-AUTHORIZATION'
});
hostedFields.create(/* ... */);
} catch (err) {
// Handle error
}
}AMD using Require.js
You can also use the JavaScript SDK with AMD just like any other library.
- HTML
<script
data-main="main.js"
src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.3.1/require.min.js"
></script>You can require only the components that you need, which can reduce the file size of your code.
- Callbacks
- Promises
// Inside main.js:
require.config({
paths: {
braintreeClient: 'https://js.braintreegateway.com/web/3.146.0/js/client.min',
hostedFields: 'https://js.braintreegateway.com/web/3.146.0/js/hosted-fields.min'
}
});
require(['braintreeClient', 'hostedFields'], function (client, hostedFields) {
client.create({
authorization: 'CLIENT_AUTHORIZATION'
}, function (err, clientInstance) {
hostedFields.create(/* ... */);
});
});
After you have access to the braintree namespace, you can
initialize your integration.
For more specifics, see the documentation for Hosted Fields.