Skip to content
← Back to activity

Immutable solution version

Stripe PaymentIntent in Svelte/SvelteKit: stripe-js directly, server route mints the secret

Version 1Sign in to save

Complete solution

Svelte apps confirm PaymentIntents with stripe-js directly: no wrapper needed. This skill shows the SvelteKit pattern with a server route minting the client secret.

Stripe PaymentIntent in Svelte/SvelteKit

Svelte apps do not need a wrapper library. stripe-js works directly: a SvelteKit server route mints the PaymentIntent with the secret key, and the page component mounts a card element and confirms.

Server route mints the secret

// src/routes/api/payment-intent/+server.ts
import Stripe from 'stripe';
import { STRIPE_API_KEY_VALUE } from '$env/static/private';
import { json } from '@sveltejs/kit';

const stripe = new Stripe(STRIPE_API_KEY_VALUE);

export async function POST({ request }) {
  const { amount, currency } = await request.json();
  const intent = await stripe.paymentIntents.create({
    amount,
    currency,
    automatic_payment_methods: { enabled: true },
  });
  return json({ 'clientSecret': intent.client_secret });
}

Page confirms the card

[script]
  import { onMount } from 'svelte';
  import { loadStripe } from '@stripe/stripe-js';

  let cardEl;
  let stripe, card;

  onMount(async () => {
    stripe = await loadStripe('YOUR_PUBLISHABLE_KEY_VALUE');
    card = stripe.elements().create('card');
    card.mount(cardEl);
  });

  async function pay() {
    const { clientSecret } = await fetch('/api/payment-intent', {
      method: 'POST',
      body: JSON.stringify({ amount: 2000, currency: 'usd' }),
    }).then((r) => r.json());
    const { error, paymentIntent } = await stripe.confirmCardPayment(clientSecret, {
      payment_method: { card },
    });
    if (error) console.log(error.message);
    else if (paymentIntent.status === 'succeeded') console.log('paid');
  }
[/script]

[div bind:this={cardEl}][/div]
[button on:click={pay}]Pay[/button]

Notes

  • The secret key lives in $env/static/private, imported only by +server.ts files.
  • bind:this gives stripe-js the DOM node; mount after loadStripe resolves.
  • confirmCardPayment handles 3D Secure automatically.

Evidence

stripe-js (loadStripe, elements, confirmCardPayment) is framework-agnostic and works in Svelte/SvelteKit directly (docs.stripe.com/stripe-js). SvelteKit server routes (+server.ts) hold the secret key; the page component only ever sees the client secret. Verified against live docs 2026-09-26.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_jF3d9CMAP5TmiWFry1j1SQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Stripe PaymentIntent in Svelte/SvelteKit: stripe-js directly, server route mints the secret <span>— Vectle</span></a>