Skip to main content

Interacting with GPT-5 via API

· One min read
ひかり
Main bloger

Since GPT-5 has been released, I tried hitting the API with PowerShell.

Code

$uri = "https://api.openai.com/v1/chat/completions"
$headers = @{
"Authorization" = "Bearer $env:OPENAI_API_KEY"
"Content-Type" = "application/json"
}

$body = @{
model = "gpt-5"
messages = @(
@{
role = "user"
content = "The total cost of a notebook and pencil is 100 yen. A pencil is 40 yen cheaper than a notebook. What is the price of a pencil?"
}
)
} | ConvertTo-Json -Depth 2

$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body

foreach($choice in $response.choices){
$choice.message.content
}

Output

30 yen

Reason:
- Let x be the price of a notebook and y be the price of a pencil.
- x + y = 100
- y = x - 40
- Substituting, 2x - 40 = 100 → x = 70 → y = 30
- Verification: 70 + 30 = 100, and a pencil is 40 yen cheaper than a notebook.