Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import stripe using node js + typescript

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

like image 527
Krishan Avatar asked Feb 02 '17 09:02

Krishan


People also ask

How do I import a stripe in TypeScript?

Usage with TypeScript import Stripe from 'stripe'; const stripe = new Stripe('sk_test_...', { apiVersion: '2022-08-01', }); const createCustomer = async () => { const params: Stripe. CustomerCreateParams = { description: 'test customer', }; const customer: Stripe. Customer = await stripe. customers.

What is stripe node JS?

Stripe Node. js Library. The Stripe Node library provides convenient access to the Stripe API from applications written in server-side JavaScript. For collecting customer and payment information in the browser, use Stripe.


2 Answers

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(); 
like image 166
Tan Nguyen Avatar answered Sep 26 '22 10:09

Tan Nguyen


Update: The solution below is outdated and for those using [email protected] 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); 
like image 20
8eecf0d2 Avatar answered Sep 22 '22 10:09

8eecf0d2