Instalacja

Kompletny przewodnik instalacji dla Chaindoc SDK. Skonfiguruj Server SDK do integracji zaplecza i Embed SDK do interfejsu podpisywania frontendu.

Wymagania wstępne

Instalacja SDK serwera

Server SDK służy do integracji zaplecza — zarządzania dokumentami, wnioskami o podpisy i weryfikacji łańcucha bloków.

Krok 1: Zainstaluj pakiet

npm install @chaindoc_io/server-sdk

Krok 2: Skonfiguruj zmienne środowiskowe

Utwórz plik .env w katalogu głównym projektu:

.env
# Production keys
CHAINDOC_SECRET_KEY=sk_live_xxxxxxxxxxxxx

# Staging/Testing keys
# CHAINDOC_SECRET_KEY=sk_test_xxxxxxxxxxxxx

# Optional: Custom API endpoint
# CHAINDOC_API_URL=https://api.chaindoc.io

Krok 3: Zainicjuj SDK

lib/chaindoc.ts
import { Chaindoc } from '@chaindoc_io/server-sdk';

// Basic initialization
export const chaindoc = new Chaindoc({
  secretKey: process.env.CHAINDOC_SECRET_KEY!,
});

// Advanced configuration
export const chaindocAdvanced = new Chaindoc({
  secretKey: process.env.CHAINDOC_SECRET_KEY!,
  baseUrl: process.env.CHAINDOC_API_URL, // Optional
  timeout: 60000, // 60 seconds
  retry: {
    maxRetries: 5,
    baseDelayMs: 1000,
    maxDelayMs: 10000,
  },
  headers: {
    'X-Custom-Header': 'value',
  },
});

Krok 4: Sprawdź połączenie

terminal
// Test API connection
const health = await chaindoc.healthCheck();

if (health.status === 'ok' && health.apiKeyValid) {
  console.log('✓ Connected to Chaindoc API');
  console.log('User ID:', health.userId);
} else {
  console.error('✗ Connection failed');
}

Instalacja SDK osadzania

Embed SDK zapewnia interfejs podpisywania dla aplikacji internetowych. Działa z dowolnym frameworkiem JavaScript.

Krok 1: Zainstaluj pakiet

npm install @chaindoc_io/embed-sdk

Krok 2: Skonfiguruj zmienne środowiskowe

# .env.local
REACT_APP_CHAINDOC_PUBLIC_KEY=pk_live_xxxxx
# or for Next.js
NEXT_PUBLIC_CHAINDOC_PUBLIC_KEY=pk_live_xxxxx

Krok 3: Zainicjuj w swojej aplikacji

// hooks/useChaindoc.ts
import { useRef, useEffect } from 'react';
import { ChaindocEmbed } from '@chaindoc_io/embed-sdk';

export function useChaindoc() {
  const sdkRef = useRef<ChaindocEmbed | null>(null);

  useEffect(() => {
    sdkRef.current = new ChaindocEmbed({
      publicKey: process.env.REACT_APP_CHAINDOC_PUBLIC_KEY!,
      environment: 'production',
    });

    return () => {
      sdkRef.current?.destroy();
    };
  }, []);

  return sdkRef.current;
}

Konfiguracja specyficzna dla frameworka

Next.js (App Router)

app/providers.tsx
'use client';

import { createContext, useContext, useEffect, useRef } from 'react';
import { ChaindocEmbed } from '@chaindoc_io/embed-sdk';

const ChaindocContext = createContext<ChaindocEmbed | null>(null);

export function ChaindocProvider({ children }: { children: React.ReactNode }) {
  const sdkRef = useRef<ChaindocEmbed | null>(null);

  useEffect(() => {
    if (!sdkRef.current) {
      sdkRef.current = new ChaindocEmbed({
        publicKey: process.env.NEXT_PUBLIC_CHAINDOC_PUBLIC_KEY!,
      });
    }

    return () => {
      sdkRef.current?.destroy();
    };
  }, []);

  return (
    <ChaindocContext.Provider value={sdkRef.current}>
      {children}
    </ChaindocContext.Provider>
  );
}

export const useChaindoc = () => useContext(ChaindocContext);

Next.js (Pages Router)

pages/_app.tsx
import type { AppProps } from 'next/app';
import { useEffect, useRef } from 'react';
import { ChaindocEmbed } from '@chaindoc_io/embed-sdk';

export default function App({ Component, pageProps }: AppProps) {
  const sdkRef = useRef<ChaindocEmbed | null>(null);

  useEffect(() => {
    sdkRef.current = new ChaindocEmbed({
      publicKey: process.env.NEXT_PUBLIC_CHAINDOC_PUBLIC_KEY!,
    });

    return () => {
      sdkRef.current?.destroy();
    };
  }, []);

  return <Component {...pageProps} />;
}

Nuxt 3

plugins/chaindoc.client.ts
import { ChaindocEmbed } from '@chaindoc_io/embed-sdk';

export default defineNuxtPlugin(() => {
  const config = useRuntimeConfig();
  
  const chaindoc = new ChaindocEmbed({
    publicKey: config.public.chaindocPublicKey,
  });

  return {
    provide: {
      chaindoc,
    },
  };
});

Angular

usługi/chaindoc.service.ts
import { Injectable, OnDestroy } from '@angular/core';
import { ChaindocEmbed } from '@chaindoc_io/embed-sdk';
import { environment } from '../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class ChaindocService implements OnDestroy {
  private sdk: ChaindocEmbed;

  constructor() {
    this.sdk = new ChaindocEmbed({
      publicKey: environment.chaindocPublicKey,
    });
  }

  getSdk(): ChaindocEmbed {
    return this.sdk;
  }

  ngOnDestroy(): void {
    this.sdk.destroy();
  }
}

Konfiguracja TypeScript

Oba zestawy SDK zawierają pełne definicje TypeScript. Nie są potrzebne żadne dodatkowe pakiety @types.

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "types": ["node"],
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

Konfiguracja środowiska

Środowisko programistyczne

terminal
// Use test keys for development
const chaindoc = new Chaindoc({
  secretKey: 'sk_test_xxxxx',
  baseUrl: 'https://api.chaindoc.io',
});

const embed = new ChaindocEmbed({
  publicKey: 'pk_test_xxxxx',
  environment: 'staging',
  debug: true, // Enable detailed logs
});

Środowisko produkcyjne

terminal
// Use live keys for production
const chaindoc = new Chaindoc({
  secretKey: process.env.CHAINDOC_SECRET_KEY!, // sk_live_xxxxx
});

const embed = new ChaindocEmbed({
  publicKey: process.env.NEXT_PUBLIC_CHAINDOC_PUBLIC_KEY!, // pk_live_xxxxx
  environment: 'production',
});

Weryfikacja

Przetestuj instalację, wykonując następujące czynności weryfikacyjne:

1Sprawdź wersję SDKconsole.log(ChaindocEmbed.version); // Powinno wyświetlić numer wersji

2Sprawdź połączenie APIPrzetestuj punkt końcowy kontroli stanu, aby potwierdzić, że klucze API są ważne.

3Przesyłanie pliku testowegoPrześlij mały plik testowy, aby sprawdzić, czy punkty końcowe multimediów działają

4Zainicjuj SDK osadzaniaZainicjuj SDK i sprawdź konsolę przeglądarki pod kątem ewentualnych błędów

Rozwiązywanie problemów

Błąd nie znaleziono modułu

terminal
# Clear cache and reinstall
rm -rf node_modules package-lock.json
npm install

# For yarn
rm -rf node_modules yarn.lock
yarn install

Błędy TypeScript

Jeśli widzisz błędy TypeScript, upewnij się, że plik tsconfig.json ma prawidłowe ustawienia rozdzielczości modułów i że zainstalowane są typy Node.js.

terminal
npm install --save-dev @types/node

Problemy z kluczem API

Kolejne kroki

  • Zapoznaj się z przewodnikiem szybkiego startu, aby przeprowadzić pierwszą integrację.
  • Sprawdź dokumentację API, aby uzyskać informacje na temat punktów końcowych.
  • Zapoznaj się z dokumentacją SDK, aby poznać zaawansowane funkcje.
  • Skonfiguruj webhooki, aby otrzymywać powiadomienia w czasie rzeczywistym.
  • Dołącz do społeczności programistów na GitHubie, aby uzyskać wsparcie.