安装
npm install @cryptonly/sdk
需要节点.js 18或以后。 TypeScript用户需要TypeScript 4.5或更晚.
快开始
import { Cryptonly } from '@cryptonly/sdk';
const client = new Cryptonly({ apiKey: process.env.CRYPTONLY_API_KEY! });
const invoice = await client.invoice.create({
amount: 100,
fiatCurrencyCode: 'USD',
description: 'Order #123',
orderId: 'order_123',
});
选项
| 选项 | 默认 | |
|---|---|---|
apiKey | 所需经费 | 您的加密 API 密钥 。 它会选择账户. |
sandbox | false | 打电话 沙盒 API而不是生产. |
baseUrl | https://api-merchant.cryptonly.net | 另一个API起源. 胜过 sandbox。 。 。 . |
timeout | 60000 | 请求超时, 以毫秒计 . |
fetch | globalThis.fetch | 习惯 fetch,用于测试或更长的运行时间. |
资源
| 客户端 | 方法 | 指南 |
|---|---|---|
client.invoice | create, (中文(简体) ), list, (中文(简体) ), get, (中文(简体) ), cancel | 发票 |
client.deposit | create, (中文(简体) ), list, (中文(简体) ), get | 存款 |
client.withdrawal | quote, (中文(简体) ), commit, (中文(简体) ), list, (中文(简体) ), get | 退 款 |
client.currencies | list, (中文(简体) ), listFiat | 支持货币 |
client.x402Settlements | list, (中文(简体) ), get | x402 付款额 |
get 财务报告和审定财务报表 cancel 带 { id } 或者说 { orderId }。 。 。 . list 方法返回 { data, total, page, limit, hasMore }。请求和响应类型从软件包中导出.
创建存款地址
const deposit = await client.deposit.create({
cryptoCurrencyCode: 'USDT_TRC20',
orderId: 'deposit_123',
});
console.log(deposit.address, deposit.qrCode);
发送退出
退出需要两次通话. quote 返回收费和短命 quoteId,以及 commit 发送它.
const quote = await client.withdrawal.quote({
amount: '50',
cryptoCurrencyCode: 'USDT_TRC20',
address: 'TJn22ewJPN89yNtrnBkagsunbrLnPyQCgs',
orderId: 'withdrawal_123',
});
const withdrawal = await client.withdrawal.commit({ quoteId: quote.quoteId });
过 allowAutoConvert: true 以弥补短缺,将余额中的其他资产转换. quote.autoConvert 然后显示转换.
列出x402次付款
const { data } = await client.x402Settlements.list({ status: 'confirmed' });
API 密钥需要 x402.list 范围.
目 录
orderId 制造 invoice.create, (中文(简体) ), deposit.create 财务报告和审定财务报表 withdrawal.quote 安全地重试。 同样的重试 orderId 返回原始,而不是创建重复。 见 一能性。 。 。 .
校验网约车
用你的账户的签名密钥 在每个网页上签名 把原始请求体递过来 x-webhook-signature 事件验证符头和密钥 :
import express from 'express';
import { verifyInvoiceWebhook } from '@cryptonly/sdk';
app.post('/webhooks/cryptonly', express.raw({ type: 'application/json' }), (req, res) => {
const result = verifyInvoiceWebhook(
req.body, // the raw Buffer, not parsed JSON
req.header('x-webhook-signature'),
process.env.CRYPTONLY_WEBHOOK_SECRET!,
);
if (!result.ok) return res.sendStatus(401);
console.log(result.envelope.data.status);
res.sendStatus(200);
});
| 活动 | 验证人 |
|---|---|
invoice.statusChanged | verifyInvoiceWebhook |
deposit.statusChanged | verifyDepositWebhook |
withdrawal.statusChanged | verifyWithdrawalWebhook |
如果失败, result.reason 实值 missing_signature, (中文(简体) ), invalid_signature, (中文(简体) ), invalid_json 或者说 wrong_event。在仪表板下获取签名密钥 警卫。见 网络用户 对有效载荷和回击.
错误
每个失败的请求都会丢出一个 CryptonlyApiError数字 :
| 外地 | |
|---|---|
status | HTTP 状态,或 0 用于网络错误和超时. |
code | 稳定错误代码, 如 exceptions.withdrawal.insufficientBalance有关这个的分支. |
message | 人文可读信息. |
extension | 额外数据,例如: { msBeforeNext } 关于费率限制. |
requestId | 联系支持时包含它 . |
path, (中文(简体) ), body | 请求路径和解析的响应正文 . |
import { CryptonlyApiError, CRYPTONLY_EXCEPTION_WITHDRAWAL_INSUFFICIENT_BALANCE } from '@cryptonly/sdk';
try {
await client.withdrawal.commit({ quoteId });
} catch (error) {
if (error instanceof CryptonlyApiError && error.code === CRYPTONLY_EXCEPTION_WITHDRAWAL_INSUFFICIENT_BALANCE) {
// top up, then retry
}
throw error;
}
错误代码导出为 CRYPTONLY_EXCEPTION_* 常数.
捐款
欢迎错误报告和调取请求 GitHub 图像。对于更大的变化,首先打开一个问题。 见 捐款额 并且,对于脆弱性, 警卫。 。 。 .