Skip to main content

Making API Calls

Once you have an endpoint and API key, you can trigger automations from anywhere.

Basic Request

curl -X POST "https://api.getgranite.ai/api/your-org/your-endpoint" \
  -H "X-Granite-API-Key: gk_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{"param1": "value1"}'

Request Format

Headers

HeaderRequiredDescription
X-Granite-API-KeyYesYour API key
Content-TypeYes (for POST)application/json

Body

JSON object with your parameters:
{
  "invoiceNumber": "INV-2024-001",
  "amount": 1500.00,
  "vendor": "Acme Supplies"
}

Response Modes

Asynchronous (Default)

Request returns immediately:
{
  "runId": "run_abc123",
  "status": "queued",
  "message": "Automation queued successfully"
}
Then poll for status:
curl "https://api.getgranite.ai/api/agent-runs/run_abc123" \
  -H "X-Granite-API-Key: gk_live_abc123..."

Synchronous

Add ?wait=true to wait for completion:
curl -X POST "https://api.getgranite.ai/api/your-org/endpoint?wait=true" \
  -H "X-Granite-API-Key: gk_live_abc123..."
Response:
{
  "runId": "run_abc123",
  "status": "completed",
  "duration": 45000,
  "result": {
    "output": "Report generated successfully",
    "filePath": "reports/january-2024.pdf"
  }
}
Synchronous calls have a timeout (default 5 minutes). For long automations, use async.

Status Values

StatusMeaning
queuedWaiting for a driver
runningCurrently executing
completedFinished successfully
failedEncountered an error
cancelledStopped manually

Error Handling

HTTP Errors

CodeMeaningAction
400Bad requestCheck parameters
401UnauthorizedCheck API key
404Not foundCheck endpoint URL
429Rate limitedWait and retry
500Server errorContact support

Execution Errors

{
  "runId": "run_abc123",
  "status": "failed",
  "error": {
    "type": "element_not_found",
    "message": "Could not locate the Submit button",
    "screenshot": "https://..."
  }
}

Code Examples

async function runAutomation(params) {
  const response = await fetch(
    'https://api.getgranite.ai/api/my-org/my-endpoint',
    {
      method: 'POST',
      headers: {
        'X-Granite-API-Key': process.env.GRANITE_API_KEY,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(params),
    }
  );

  if (!response.ok) {
    throw new Error(`HTTP ${response.status}`);
  }

  return response.json();
}

// Usage
const result = await runAutomation({
  month: 'January',
  year: 2024,
});
console.log('Run ID:', result.runId);

Polling for Status

For async calls, poll until completion:
async function waitForCompletion(runId) {
  while (true) {
    const response = await fetch(
      `https://api.getgranite.ai/api/agent-runs/${runId}`,
      {
        headers: { 'X-Granite-API-Key': apiKey },
      }
    );
    const data = await response.json();

    if (['completed', 'failed', 'cancelled'].includes(data.status)) {
      return data;
    }

    await new Promise(r => setTimeout(r, 2000)); // Wait 2 seconds
  }
}

Webhooks

For event-driven workflows, you can set up a webhook to be called when execution completes. (Coming soon)

Next Steps