Skip to content

Getting Started: PHP

Use PHP with cURL to call the BetterPrompt API.

php
<?php
$apiKey = getenv('BETTERPROMPT_API_KEY') ?: 'YOUR_API_KEY';
$promptVersionId = getenv('BP_PROMPT_VERSION_ID') ?: 'YOUR_PROMPT_VERSION_ID';

$ch = curl_init('https://api.betterprompt.me/v1/runs');
$payload = json_encode([
  'promptVersionId' => $promptVersionId,
  'inputs' => [ 'textInputs' => [ 'yourField' => 'value' ] ],
]);

curl_setopt_array($ch, [
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer ' . $apiKey,
    'Content-Type: application/json'
  ],
  CURLOPT_POSTFIELDS => $payload,
  CURLOPT_RETURNTRANSFER => true,
]);

$response = curl_exec($ch);
if ($response === false) {
  throw new Exception(curl_error($ch));
}

echo $response, "\n";