ドキュメントの生成
ドキュメントの生成
POST /2.0/docgen_batches
エンドポイントを使用すると、Box Doc Genテンプレートを入力データとして使用してドキュメントを生成できます。
前提条件
Box Doc Gen APIの使用を開始する前に、Box Doc Genの使い方ガイドに記載されている手順に従って、カスタムアプリとBox Doc Genテンプレートを作成してください。
リクエストの送信
ドキュメント (複数可) を生成するには、POST /2.0/docgen_batches
エンドポイントを使用します。
パラメータ
コールを実行するには、以下のパラメータを渡す必要があります。必須のパラメ ータは太字で示されています。
パラメータ | 説明 | 例 |
---|---|---|
file.id | Box Doc Genテンプレートとして設定するファイルのID。 | 12345678 |
file.type | 指定した入力データの種類。値は常に**file **になります。 | file |
file_version | テンプレートのファイルバージョン。 | 12345 |
input_source | 生成されるドキュメントの入力ソース。この値は、APIベースのすべてのドキュメント生成リクエストでapi にする必要があります。 | api |
output_type | 出力ファイルの種類。 | docx |
destination_folder.id | 生成されたドキュメントが保存されるフォルダのID。 | 12345678 |
destination_folder.type | 保存先の項目の種類。生成されたファイルはフォルダに保存されるため、値は常に**folder **になります。 | file |
document_generation_data.generated_file_name | 生成されるファイルの名前。 | New_Template |
document_generation_data.user_input | ドキュメントの生成に使用するJSONデータ。 | {"id": 2, "name": "Ink Cartridge", "type": "non-fragile"} |
ユースケース
Box Doc GenテンプレートとJSONデータを準備できたら、Box Doc Gen APIにドキュメント生成のリクエストを行うことができます。
コールのサンプルは次のようになります。
cURL
curl -L 'https://api.box.com/2.0/docgen_batches' \
-H 'box-version: 2025.0' \
-H 'Authorization: Bearer <ACCESS_TOKEN>' \
-D '{
"file": {
"id": "12345678",
"type": "file"
},
"input_source": "api",
"destination_folder": {
"id": "12345678",
"type": "folder"
},
"output_type": "docx",
"document_generation_data": [
{
"generated_file_name": "Image test",
"user_input": {
"order": {
"id": "12305",
"date": "18-08-2023",
"country": "US",
"expiryDate": "18-08-2024",
"currency": "$",
"amount": 5060.5,
"taxRate": 10,
"requester": "John",
"approver": "Smith",
"department": "Procurement",
"paymentTerms": "30 days",
"deliveryTerms": "30 days",
"deliveryDate": "18-09-2023",
"vendor": {
"company": "Example company",
"address": {
"street": "Example street",
"city": "Example city",
"zip": "EX-456"
}
},
"products": [
{
"id": 1,
"name": "A4 Papers",
"type": "non-fragile",
"quantity": 100,
"price": 29,
"amount": 2900
},
{
"id": 2,
"name": "Ink Cartridge",
"type": "non-fragile",
"quantity": 40,
"price": 39,
"amount": 1560
},
{
"id": 3,
"name": "Adhesive tape",
"type": "non-fragile",
"quantity": 20,
"price": 30,
"amount": 600.5
}
]
}
}
}
]`
TypeScript Gen
await client.docgen.createDocgenBatchV2025R0({
file: new FileReferenceV2025R0({ id: uploadedFile.id }),
inputSource: 'api',
destinationFolder: new DocGenBatchCreateRequestV2025R0DestinationFolderField({
id: folder.id,
}),
outputType: 'pdf',
documentGenerationData: [
{
generatedFileName: 'test',
userInput: { ['abc']: 'xyz' },
} satisfies DocGenDocumentGenerationDataV2025R0,
],
} satisfies DocGenBatchCreateRequestV2025R0);
Python Gen
client.docgen.create_docgen_batch_v2025_r0(
FileReferenceV2025R0(id=uploaded_file.id),
"api",
CreateDocgenBatchV2025R0DestinationFolder(id=folder.id),
"pdf",
[
DocGenDocumentGenerationDataV2025R0(
generated_file_name="test", user_input={"abc": "xyz"}
)
],
)
.NET Gen
await client.Docgen.CreateDocgenBatchV2025R0Async(requestBody: new DocGenBatchCreateRequestV2025R0(file: new FileReferenceV2025R0(id: uploadedFile.Id), inputSource: "api", destinationFolder: new DocGenBatchCreateRequestV2025R0DestinationFolderField(id: folder.Id), outputType: "pdf", documentGenerationData: Array.AsReadOnly(new [] {new DocGenDocumentGenerationDataV2025R0(generatedFileName: "test", userInput: new Dictionary<string, object>() { { "abc", "xyz" } })})));
リクエストが処理されている間、document_generation_data
配列の各エントリは個別のドキュメント生成ジョブとして処理され、そのジョブは、Box Doc Genによってドキュメント生成キューに追加されます。
生成されたドキュメントは、指定したフォルダに保存されます。