Setup Application

This section guides you through setting up ngx-stripe in your Angular application, explaining how to configure the library for optimal integration with Stripe's payment processing capabilities.

This doc assumes that you already have a basic working knowledge of Angular and that you have already set up an Angular project. If you're new to Angular, we recommend that you take a look at the new Angular Documention site.

Initializing

Ngx Stripe initialization is designed to align closely with the StripeJS initialization process. The parameters you pass to ngx-stripe's forRoot method or provideNgxStripe are equivalent to those you would pass to the Stripe constructor as per StripeJS documentation.

When initializing Ngx Stripe, you can pass the following parameters:

  • Stripe Publishable Key: Your Stripe account's publishable key. This key is used to identify your Stripe account and to tokenize payment information client-side before it's sent to Stripe's servers.
  • StripeJS Options: Additional options for configuring the StripeJS instance, such as locale, apiVersion or stripeAccount.

If you're using standalone architecture, you can use provideNgxStripe to setup the services. This is the recommended way to do it. ng add ngx-stripe adds it for you.

import { provideNgxStripe } from 'ngx-stripe';

bootstrapApplication(AppComponent, {
  providers: [provideNgxStripe('***your-stripe-publishable-key***')]
});

or if you're still using modules, you do it as usual using forRoot

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

// Import your library
import { NgxStripeModule } from 'ngx-stripe';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgxStripeModule.forRoot('***your-stripe-publishable-key***'),
    LibraryModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Dynamic Key Management

It's important to note that passing the Stripe publishable key and options during initialization is not mandatory. In scenarios where your Stripe keys are fetched dynamically, or you need to initialize Stripe instances with different configurations, you have two flexible options:

  1. Use injectStripe: Creates a dedicated Stripe instance when you already have the publishable key in the component. You must bind that instance to ngx-stripe directives and components with [stripe]="stripe". Calling injectStripe in a constructor without assigning the result, or omitting [stripe], leaves the default StripeService uninitialized and Elements will fail to load. See also the Elements documentation and the Payment Element with injectStripe example.
  2. import { Component } from '@angular/core';
    
    import { StripeElementsOptions } from '@stripe/stripe-js';
    import { injectStripe, StripePaymentElementComponent } from 'ngx-stripe';
    
    @Component({
      selector: 'app-checkout',
      template: `
        <ngx-stripe-payment [stripe]="stripe" [elementsOptions]="elementsOptions" />
      `,
      standalone: true,
      imports: [StripePaymentElementComponent]
    })
    export class CheckoutComponent {
      stripe = injectStripe(environment.stripePublishableKey);
      elementsOptions: StripeElementsOptions = {
        clientSecret: '*** from your server ***'
      };
    }
  3. Use StripeService.changeKey: When the key arrives asynchronously after app bootstrap, register ngx-stripe without a key (provideNgxStripe() or NgxStripeModule.forRoot()), call changeKey on the injected StripeService, then render Stripe UI (for example with @if or *ngIf) so Elements are not created before the key is set.
  4. import { Component, inject, OnInit, signal } from '@angular/core';
    
    import { StripeElementsOptions } from '@stripe/stripe-js';
    import { StripePaymentElementComponent, StripeService } from 'ngx-stripe';
    
    @Component({
      selector: 'app-checkout',
      template: `
        @if (ready()) {
          <ngx-stripe-payment [elementsOptions]="elementsOptions" />
        }
      `,
      standalone: true,
      imports: [StripePaymentElementComponent]
    })
    export class CheckoutComponent implements OnInit {
      private stripeService = inject(StripeService);
    
      ready = signal(false);
      elementsOptions: StripeElementsOptions = {
        clientSecret: '*** from your server ***'
      };
    
      ngOnInit() {
        this.keyService.fetchPublishableKey().then((key) => {
          this.stripeService.changeKey(key);
          this.ready.set(true);
        });
      }
    
      constructor(private keyService: YourKeyService) {}
    }
  5. Stripe Service Factory: Use StripeFactoryService to create additional Stripe instances on demand (multiple accounts or configurations). See the Elements documentation (Classic Angular tab).