Skip to main content


Affirm Merchant Help

Initiate Checkout

Embed Affirm's JS runtime code

  _affirm_config = {
    public_api_key:  "XXXXXXXXXXXXXXX",
    script:          ""
  (function(l,g,m,e,a,f,b){var d,c=l[m]||{},h=document.createElement(f),n=document.getElementsByTagName(f)[0],k=function(a,b,c){return function(){a[b]._.push([c,arguments])}};c[e]=k(c,e,"set");d=c[e];c[a]={};c[a]._=[];d._=[];c[a][b]=k(c,a,b);a=0;for(b="set add save post open empty reset on off trigger ready setProduct".split(" ");a<b.length;a++)d[b[a]]=k(c,e,b[a]);a=0;for(b=["get","token","url","items"];a<b.length;a++)d[b[a]]=function(){};h.async=!0;h.src=g[f];n.parentNode.insertBefore(h,n);delete g[f];d(g);l[m]=c})(window,_affirm_config,"affirm","checkout","ui","script","ready");
 // Use your live public API Key and script to point to Affirm production environment.

Note: Be sure to use your public API key from the sandbox merchant dashboard for 'public_api_key' and a session ID variable for the optional 'session_id' to take advantage of additional analytics. The API key must match the Affirm-environment you're referencing ('sandbox' or 'live').

Once the Affirm JS is initialized, the affirm.checkout method is made available:

  • affirm.checkout.open_vcn() - Initiates the VCN checkout window and accepts the checkout data object as its parameter.

The Affirm environment that's used is determined by the Affirm script URL that's defined in the _affirm_config.script:

var _affirm_config = {
    public_api_key:  "XXXXXXXXXXXXXXX",
    script:          ""

Checkout Object

You can define a separate checkout object to contain the top-level parameters for the open_vcn() method. This is where you define the name of the checkout_data object, success callback, and error callback.


      success: function(card_token) {
      error: error_callback_function
      checkout_session_token: checkoutSessionToken
required event. This event is triggered when the user successfully completes the Affirm checkout flow. The event data contains the virtual card token. This token will then be subsequently exchanged for the virtual card details via server-side API.
optional event. This event is triggered if the user is denied, or if they voluntarily cancel the Affirm checkout flow. The event data is generic, and only indicates that the checkout was canceled (even if they were actually denied).
required string. The checkout session token corresponds to checkout data submitted to the Affirm servers previously, which contains all the cart details, customer information, and metadata for the order.

  • Was this article helpful?