PAY.JP Node.js SDK for v2 API. This library provides a TypeScript-first client for integrating with the PAY.JP payment platform.
- 🚀 TypeScript Support: Full type definitions for all API endpoints
- 🔐 Secure: Built-in authentication with API key management
- 📦 Modern: Uses fetch API with
@hey-api/client-fetch - 🧪 Well Tested: Comprehensive test coverage
- 📚 Auto-generated: API client generated from OpenAPI specification
npm install @payjp/payjpv2yarn add @payjp/payjpv2pnpm install @payjp/payjpv2import { createClient } from '@payjp/payjpv2';
const client = createClient({
apiKey: 'sk_test_xxxxxxxxxxxx', // Your PAY.JP API Key
baseUrl: 'https://api.pay.jp', // Optional: defaults to PAY.JP API
});import { createCustomer, getCustomer, updateCustomer } from '@payjp/payjpv2';
// Create a customer
const result = await createCustomer({
client,
body: {
email: 'customer@example.com',
description: 'New Customer',
}
});
if (result.error) {
console.error('Error:', result.error);
} else {
console.log('Customer created:', result.data);
// Get customer details
const customer = await getCustomer({
client,
path: { customer_id: result.data.id }
});
console.log('Retrieved customer:', customer.data);
}import {
createPaymentFlow,
confirmPaymentFlow,
capturePaymentFlow
} from '@payjp/payjpv2';
// Create a payment flow
const paymentFlow = await createPaymentFlow({
client,
body: {
amount: 1000, // Amount in yen (e.g., 1000 = ¥1,000)
}
});
if (paymentFlow.data) {
// Confirm the payment flow with a payment method
const confirmed = await confirmPaymentFlow({
client,
path: { payment_flow_id: paymentFlow.data.id },
body: {
payment_method: 'pm_xxxxxxxxxxxx'
}
});
console.log('Payment confirmed:', confirmed.data);
}import { createProduct, createPrice } from '@payjp/payjpv2';
// Create a product
const product = await createProduct({
client,
body: {
name: 'Premium Plan',
}
});
// Create a price for the product
if (product.data) {
const price = await createPrice({
client,
body: {
unit_amount: 1500,
currency: 'jpy',
product: product.data.id,
}
});
console.log('Price created:', price.data);
}The SDK provides functions for all PAY.JP v2 API endpoints:
createCustomer()- Create a new customergetCustomer()- Retrieve customer detailsupdateCustomer()- Update customer informationdeleteCustomer()- Delete a customergetAllCustomers()- List all customers
createPaymentFlow()- Create a payment flowgetPaymentFlow()- Get payment flow detailsupdatePaymentFlow()- Update payment flowconfirmPaymentFlow()- Confirm a payment flowcapturePaymentFlow()- Capture an authorized paymentcancelPaymentFlow()- Cancel a payment flowgetAllPaymentFlows()- List payment flows
createPaymentMethod()- Create a payment methodgetPaymentMethod()- Retrieve payment method detailsupdatePaymentMethod()- Update payment methodgetAllPaymentMethods()- List payment methods
createProduct(),getProduct(),updateProduct(),deleteProduct()createPrice(),getPrice(),updatePrice()
createPaymentRefund()- Create a refundgetPaymentRefund()- Get refund detailsupdatePaymentRefund()- Update refundgetAllPaymentRefunds()- List refunds
createSetupFlow()- Create a setup flowgetSetupFlow()- Get setup flow detailsupdateSetupFlow()- Update setup flowconfirmSetupFlow()- Confirm a setup flowcancelSetupFlow()- Cancel a setup flowgetAllSetupFlows()- List setup flows
createCheckoutSession(),getCheckoutSession(),updateCheckoutSession()
The SDK returns errors in a consistent format:
import { createCustomer } from '@payjp/payjpv2';
const result = await createCustomer({
client,
body: {
email: 'customer@example.com'
}
});
if (result.error) {
console.error('API Error:', result.error);
} else {
console.log('Success:', result.data);
}invalid_request_error- Invalid parameters or malformed requestauthentication_error- Invalid API keypermission_error- Insufficient permissionsrate_limit_error- Too many requestsapi_error- Internal server error
For security, store your API key in environment variables:
# .env file
PAYJP_API_KEY=sk_test_xxxxxxxxxxxximport { createClient } from '@payjp/payjpv2';
const client = createClient({
apiKey: process.env.PAYJP_API_KEY!,
});For testing or custom endpoints:
const client = createClient({
apiKey: 'sk_test_xxxxxxxxxxxx',
baseUrl: 'https://custom-api.example.com'
});The SDK is built with TypeScript and provides full type safety:
import type { CustomerResponse, PaymentFlowResponse } from '@payjp/payjpv2';
import { createCustomer } from '@payjp/payjpv2';
// Types are automatically inferred from API responses
const result = await createCustomer({
client,
body: {
email: 'test@example.com'
}
});
if (result.data) {
const customer: CustomerResponse = result.data;
console.log(customer.id);
}The SDK includes comprehensive tests. To run them:
npm test # Run tests
npm run build # Build the project
npm run lint # Type check- Node.js 20 or higher
- TypeScript 5.0+ (for TypeScript projects)
MIT License - see LICENSE for details.