Skip to content

anthropic-version header is required #2

@richardtape

Description

@richardtape

Hi!

Thanks for making this.

It looks like the Anthropic API has updated since you produced this. The "anthropic-version header is required" error is sent back when you use your library as-is.

Adding

$this->version = $version;

to your constructor, and then

'anthropic-version: ' . $this->version

to the CURLOPT_HTTPHEADER does the trick. So the full class would be

<?php

namespace Alle_AI\Anthropic;

class AnthropicAPI {
  private $api_key;
	private $version;

  public function __construct($api_key, $version) {
    $this->api_key = $api_key;
		$this->version = $version;
  }

  public function generateText($data) {

    $options = array(
      CURLOPT_URL => 'https://api.anthropic.com/v1/complete',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HTTPHEADER => array(
        'x-api-key: ' . $this->api_key,
        'content-type: application/json',
				'anthropic-version: ' . $this->version
      ),
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => json_encode($data)
    );

    $curl = curl_init();
    curl_setopt_array($curl, $options);
    $api_response = curl_exec($curl);
    curl_close($curl);

    $response = json_decode($api_response, true);

    return $response;
  }
}

I'm okay to produce a Pull Request for this should you wish.

(There's also a minor error with the readme which prevents this working as-is. I'll send a separate Issue on that)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions