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

Bybit REST REST V5 p2p example for the Siebly Bybit SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.

What This Example Covers

  • Bybit REST API example in TypeScript.
  • Uses the Siebly Bybit SDK package bybit-api instead of hand-written HTTP request plumbing.
  • Source path: Bybit/Rest/rest-v5-p2p.ts.
  • Example category: REST.
  • 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.

Example Path

Bybit/Rest/rest-v5-p2p.ts

Source Link

Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Bybit/Rest/rest-v5-p2p.ts

Related SDK Docs

Example Source

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();