Bybit JavaScript SDK example: rest-v5-p2p.ts

Bybit REST V5 p2p JavaScript example for the Siebly Bybit SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Bybit/Rest/rest-v5-p2p.ts

What this example covers

  • Bybit REST API JavaScript example.
  • Uses the Siebly Bybit SDK package bybit-api instead of hand-written HTTP request plumbing.
  • Source path: Bybit/Rest/rest-v5-p2p.ts.
  • Imports SDK symbols including RestClientV5.
  • Calls SDK methods such as getP2POrders(), uploadP2PChatFile().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { RestClientV5 } from 'bybit-api';import fs from 'fs';import path from 'path'; // ENDPOINT: /v5/p2p/oss/upload_file// METHOD: POST// PUBLIC: NO// NOTE: Node.js only (Buffer required) const key = process.env.API_KEY_COM;const secret = process.env.API_SECRET_COM; const client = new RestClientV5({  key: key,  secret: secret,}); async function uploadP2PChatFile() {  try {    // You must read the file yourself and pass the Buffer + filename    const filePath = './docs/images/logo1.png';    const fileBuffer = fs.readFileSync(filePath);     /**     *     *     *     *     *     *     */     // Test basic P2P API connectivity    const result0 = await client.getP2POrders({      page: 1,      size: 1,    });    console.log('Get P2P orders result:', result0);     // Example 1: Upload from file path    const result1 = await client.uploadP2PChatFile({      fileBuffer: fileBuffer,      fileName: path.basename(filePath), // Extract filename from path    });    console.log(      'Upload from file path result:',      JSON.stringify(result1, null, 2),    );     // Example 2: Upload with custom filename    // You control the filename sent to Bybit    const result2 = await client.uploadP2PChatFile({      fileBuffer: fileBuffer,      fileName: 'custom-name.png', // Use any filename you want    });    console.log('Upload with custom filename result:', result2);     // Example 3: Upload different file    const pdfBuffer = fs.readFileSync('./document.pdf');    const result3 = await client.uploadP2PChatFile({      fileBuffer: pdfBuffer,      fileName: 'document.pdf',    });    console.log('Upload PDF result:', result3);     // Supported file types (determined by filename extension):    // - Images: jpg, jpeg, png    // - Documents: pdf    // - Videos: mp4  } catch (e) {    console.error('Error uploading file:', e);  }} uploadP2PChatFile(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.