47

I need to import stripe in to my application

First i installed stripe npm package

npm install stripe --save

Stripe documentation says that secret key should be set before connect the api.

In Node it likes this

var stripe = require('stripe')(' your stripe API key ');

I need to convert it to typescript

I tried following way . but it didnt work for me

import * as stripe from 'stripe';
stripe('sk_test_...')

if some can help me to solve this problem it will great help to me to continue my project without delay.

Thank you

Ywain
  • 16,854
  • 4
  • 51
  • 67
Krishan
  • 2,356
  • 6
  • 36
  • 47
  • Have you checked http://stackoverflow.com/questions/18378503/importing-node-modules-with-typescript?rq=1 ? – bug-a-lot Feb 02 '17 at 14:18

6 Answers6

64

You can refer to the repo on GitHub: https://github.com/stripe/stripe-node

import Stripe from 'stripe';

const stripe = new Stripe('sk_test_...', {
  apiVersion: '2020-08-27',
});

const createCustomer = async () => {
  const params: Stripe.CustomerCreateParams = {
    description: 'test customer',
  };

  const customer: Stripe.Customer = await stripe.customers.create(params);

  console.log(customer.id);
};
createCustomer();
Joe Previte
  • 114
  • 1
  • 16
Tan Nguyen
  • 947
  • 7
  • 8
31

As of stripe 8.0.1 the package has its own typeing so it is not necessary to install additional types. Just import it like this:

import Stripe from 'stripe';

David Dehghan
  • 22,159
  • 10
  • 107
  • 95
29

Update: The solution below is outdated and for those using stripe@8.0.1 or greater the answer from David Dehghan should be used.

As britzkopf said, stripe don't provide their own definitions yet (and probably never will), but you can use the type definitions from @types/stripe.

npm install stripe @types/stripe

And then import and construct the Stripe class as follows.

import * as Stripe from 'stripe';
const stripe = new Stripe('xxx_xxx_xxx');

If you want finer grained imports for some reason you can use this (somewhat hacky) approach instead.

import { resources } from 'stripe';
const stripeData = require('stripe')('xxx_xxx_xxx');
const customers = new resources.Customers(stripeData, null);
8eecf0d2
  • 1,569
  • 1
  • 13
  • 23
10

I had the same problem and the solution provided did not work for me:

import * as Stripe from 'stripe';
const stripe = new Stripe('xxx_xxx_xxx');

Using this approach, I got this error

[ts] Cannot use 'new' with an expression whose type lacks a call or construct signature. stripe.ts(1, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead. (alias) class Stripe (alias) namespace Stripe import Stripe

I got it working using "allowSyntheticDefaultImports": true in tsconfig.json With this compile option, the following is valid in TypeScript:

import Stripe from "stripe";

const secret = process.env.STRIPE_SECRET!;
export const stripe = new Stripe(secret);
  • This worked for me, thanks! `"allowSyntheticDefaultImports: true"` was the default in my case so I did not need to change anything. – damix911 Nov 09 '19 at 09:30
8

It's a feature request. Go give it another thumbs up.

britzkopf
  • 168
  • 1
  • 8
1

From the official documentation:

import Stripe from 'stripe';
const stripe = new Stripe('sk_test_...');
chris
  • 2,490
  • 4
  • 32
  • 56