anthropic_sdk_dart 0.0.1 copy "anthropic_sdk_dart: ^0.0.1" to clipboard
anthropic_sdk_dart: ^0.0.1 copied to clipboard

Dart Client for the Anthropic API (Claude 3 Opus, Sonnet, Haiku, etc.).

Anthropic Dart Client #

tests anthropic_sdk_dart MIT

Unofficial Dart client for Anthropic API (aka Claude API).

Features #

  • Fully type-safe, documented and tested
  • All platforms supported (including streaming on web)
  • Custom base URL, headers and query params support (e.g. HTTP proxies)
  • Custom HTTP client support (e.g. SOCKS5 proxies or advanced use cases)

Supported endpoints:

  • Messages (with streaming support)

Table of contents #

Usage #

Refer to the documentation for more information about the API.

Authentication #

The Anthropic API uses API keys for authentication. Visit the Anthropic console to retrieve the API key you'll use in your requests.

Remember that your API key is a secret!
Do not share it with others or expose it in any client-side code (browsers, apps). Production requests must be routed through your own backend server where your API key can be securely loaded from an environment variable or key management service.

final apiKey = Platform.environment['ANTHROPIC_API_KEY'];
final client = AnthropicClient(apiKey: apiKey);

Messages #

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

Create a Message:

final res = await client.createMessage(
  request: CreateMessageRequest(
    model: Model.model(Models.claude3Opus20240229),
    maxTokens: 1024,
    messages: [
      Message(
        role: MessageRole.user,
        content: 'Hello, Claude',
      ),
    ],
  ),
);
print(res.content.text);
// Hi there! How can I help you today?

Model is a sealed class that offers two ways to specify the model:

  • Model.modelId('model-id'): the model ID as string (e.g. 'claude-3-haiku-20240307').
  • Model.model(Models.claude3Opus20240229): a value from Models enum which lists all the available models.

Mind that this list may not be up-to-date. Refer to the documentation for the updated list.

Streaming messages:

final stream = await client.createMessageStream(
  request: CreateMessageRequest(
    model: Model.model(Models.claude3Opus20240229),
    maxTokens: 1024,
    messages: [
      Message(
        role: MessageRole.user,
        content: 'Hello, Claude',
      ),
    ],
  ),
);
String text = '';
await for (final res in stream) {
  res.map(
      messageStart: (e) {},
      messageDelta: (e) {},
      messageStop: (e) {},
      contentBlockStart: (e) {},
      contentBlockDelta: (e) {
        text += e.delta.text;
      },
      contentBlockStop: (e) {},
      ping: (e) {},
  );
}
print(text);
// Hi there! How can I help you today?

Advance Usage #

Default HTTP client #

By default, the client uses https://api.anthropic.com/v1 as the baseUrl and the following implementations of http.Client:

Custom HTTP client #

You can always provide your own implementation of http.Client for further customization:

final client = AnthropicClient(
  apiKey: 'MISTRAL_API_KEY',
  client: MyHttpClient(),
);

Using a proxy #

HTTP proxy

You can use your own HTTP proxy by overriding the baseUrl and providing your required headers:

final client = AnthropicClient(
  baseUrl: 'https://my-proxy.com',
  headers: {
      'x-my-proxy-header': 'value',
  },
);

If you need further customization, you can always provide your own http.Client.

SOCKS5 proxy

To use a SOCKS5 proxy, you can use the socks5_proxy package:

final baseHttpClient = HttpClient();
SocksTCPClient.assignToHttpClient(baseHttpClient, [
  ProxySettings(InternetAddress.loopbackIPv4, 1080),
]);
final httpClient = IOClient(baseClient);

final client = AnthropicClient(
  client: httpClient,
);

Acknowledgements #

The generation of this client was made possible by the openapi_spec package.

License #

Anthropic Dart Client is licensed under the MIT License.