Skip to main content

OpenAI API Integration

Use the Json2doc MCP server with OpenAI's API to generate documents programmatically.

Prerequisites

Installation

pip install openai

Basic Usage

from openai import OpenAI

client = OpenAI(api_key="your-openai-api-key")

response = client.responses.create(
model="gpt-4o",
tools=[
{
"type": "mcp",
"server_label": "json2doc",
"server_url": "https://mcp.json2doc.com/mcp",
"require_approval": "never",
"headers": {
"x-api-key": "your-json2doc-api-key"
}
}
],
input="Create a PDF document with a title 'Project Report' and a paragraph about our Q4 results.",
)

print(response.output_text)
warning

Replace your-openai-api-key and your-json2doc-api-key with your actual API keys.

Configuration Options

{
"type": "mcp", # Required: Must be "mcp"
"server_label": "json2doc", # Label for the MCP server
"server_url": "https://mcp.json2doc.com/mcp", # MCP server endpoint
"require_approval": "never", # "never", "always", or "auto"
"headers": {
"x-api-key": "your-json2doc-api-key" # Your Json2doc API key
}
}

Approval Modes:

  • "never" - AI uses tools without approval (recommended for automation)
  • "always" - AI asks for approval before using tools
  • "auto" - AI decides when to ask for approval

Examples

Example 1: Generate Invoice

from openai import OpenAI

client = OpenAI(api_key="your-openai-api-key")

invoice_data = {
"customer": "Acme Corp",
"amount": 2500.00,
"items": [
{"description": "Consulting Services", "amount": 2000.00},
{"description": "Support Package", "amount": 500.00}
]
}

response = client.responses.create(
model="gpt-4o",
tools=[
{
"type": "mcp",
"server_label": "json2doc",
"server_url": "https://mcp.json2doc.com/mcp",
"require_approval": "never",
"headers": {
"x-api-key": "your-json2doc-api-key"
}
}
],
input=f"Create a professional invoice PDF for {invoice_data['customer']} "
f"with total amount ${invoice_data['amount']}. "
f"Include these items: {invoice_data['items']}",
)

print(response.output_text)

Example 2: Batch Document Generation

from openai import OpenAI

client = OpenAI(api_key="your-openai-api-key")

customers = ["Acme Corp", "TechStart Inc", "Global Solutions"]

for customer in customers:
response = client.responses.create(
model="gpt-4o",
tools=[
{
"type": "mcp",
"server_label": "json2doc",
"server_url": "https://mcp.json2doc.com/mcp",
"require_approval": "never",
"headers": {
"x-api-key": "your-json2doc-api-key"
}
}
],
input=f"Generate a monthly status report PDF for {customer}. "
f"Include sections for: Overview, Key Metrics, Achievements, Next Steps.",
)

print(f"Generated report for {customer}: {response.output_text}")

Troubleshooting

Authentication Errors

Solutions:

  • Verify your Json2doc API key in the dashboard
  • Check the header name is x-api-key
  • Ensure no extra spaces in the key
  • Verify the key hasn't expired

Connection Issues

Solutions:

  • Verify the URL: https://mcp.json2doc.com/mcp
  • Check your internet connection
  • Ensure firewall allows the connection

Rate Limiting

Solutions:

  • Check your Json2doc plan limits in the dashboard
  • Implement exponential backoff in your code
  • Monitor your usage