API Online

SDK Examples

Ready-to-use code snippets for integrating with the GreenLedger API.

Installation

npm install axios

Full SDK Example

import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:8000/api',
  headers: { 'Content-Type': 'application/json' }
})

// Upload a document
async function uploadDocument(file: File, loanId?: string) {
  const formData = new FormData()
  formData.append('file', file)
  if (loanId) formData.append('loan_id', loanId)
  
  const { data } = await api.post('/documents/upload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

// Get all clauses
async function getClauses(category?: string) {
  const { data } = await api.get('/clauses/', {
    params: { category }
  })
  return data.clauses
}

// Review a clause
async function reviewClause(clauseId: number, status: 'APPROVED' | 'REJECTED') {
  const { data } = await api.patch(`/clauses/${clauseId}/review`, {
    review_status: status
  })
  return data
}

// Create obligation from clause
async function createObligation(clauseId: number, kpi: string, targetValue: string) {
  const { data } = await api.post('/compliance/obligations', {
    clause_id: clauseId,
    kpi,
    target_value: targetValue
  })
  return data
}

// Chat with documents
async function chat(query: string) {
  const { data } = await api.post('/chat/', { query })
  return data
}

// Usage
const clauses = await getClauses('Environmental')
console.log(clauses)