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
Initializing
Ngx Stripe initialization is designed to align closely with the StripeJS initialization process. The parameters you pass to ngx-stripe's
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
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
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:
- 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" . CallinginjectStripe in a constructor without assigning the result, or omitting[stripe] , leaves the defaultStripeService uninitialized and Elements will fail to load. See also theElements documentation and thePayment Element with injectStripe example . - Use
StripeService.changeKey : When the key arrives asynchronously after app bootstrap, register ngx-stripe without a key (provideNgxStripe() orNgxStripeModule.forRoot() ), callchangeKey on the injectedStripeService , then render Stripe UI (for example with@if or*ngIf ) so Elements are not created before the key is set. - Stripe Service Factory: Use
StripeFactoryService to create additional Stripe instances on demand (multiple accounts or configurations). See theElements documentation (Classic Angular tab).
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 ***'
};
}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) {}
}