PacteryDevelopers

Node.js 전체 예제

fetch만으로 전체 흐름을 구성한 예제입니다.

const API = 'https://api.pactery.com/v1'
const KEY = process.env.PACTERY_API_KEY

async function pactery(path, init = {}) {
  const res = await fetch(API + path, {
    ...init,
    headers: {
      Authorization: `Bearer ${KEY}`,
      'Content-Type': 'application/json',
      ...(init.headers || {}),
    },
  })
  if (!res.ok) throw new Error((await res.json()).error?.message || res.status)
  return res.json()
}

// 1) 문서 생성
const doc = await pactery('/documents', {
  method: 'POST',
  body: JSON.stringify({ title: '업무 위탁 계약서', content: '<h1>계약서</h1><p>...</p>' }),
})

// 2) 서명자 지정
const signer = await pactery(`/documents/${doc.id}/participants`, {
  method: 'POST',
  body: JSON.stringify({ name: '홍길동', phone: '01012345678', role: 'signer' }),
})

// 3) 서명 필드 배치
await pactery(`/documents/${doc.id}/fields`, {
  method: 'PUT',
  body: JSON.stringify({
    fields: [{ participant_id: signer.id, type: 'signature', page: 1, x: 0.62, y: 0.8, width: 0.25, height: 0.08 }],
  }),
})

// 4) 발송
await pactery(`/documents/${doc.id}/send`, { method: 'POST', body: '{}' })
console.log('발송 완료:', doc.id)