# Developer API

SleekAI defines a few global PHP functions. They exist only while SleekAI is
active, so guard calls with `function_exists()` in code that can run without
it. Its hooks, WP-CLI commands and REST operations are listed in the
generated references: [hooks](/docs/ai/reference/hooks.md),
[CLI](/docs/ai/reference/cli.md) and [REST](/docs/ai/reference/rest.md).

## Generate a response

`sleekAi_generate_response()` sends one request to a provider with the stored
API key and waits for the complete reply.

```php
if (function_exists('sleekAi_generate_response')) {
    $response = sleekAi_generate_response([
        'model' => 'openai-gpt-4.1-nano',
        'messages' => [
            [
                'role' => 'system',
                'content' => 'You are a helpful assistant.',
            ],
            [
                'role' => 'user',
                'content' => 'What is 1 + 1?',
            ],
        ],
    ]);

    if ($response['success']) {
        $answer = $response['content'];
    } else {
        error_log('SleekAI: ' . $response['error']);
    }
}
```

### Arguments

| Key                                     | Description                                                                                                                                                                            |
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model`                                 | The provider and model, see below. Required unless `chatbotId` names a chatbot with a model.                                                                                           |
| `messages`                              | Required. A list of messages, each with a `role` (`system`, `user` or `assistant`) and a `content` string.                                                                             |
| `max_tokens`                            | The longest reply, in tokens. Without it, OpenRouter requests use `2048` and Anthropic requests `8192`; the other providers use their own default.                                     |
| `temperature`, `top_p`                  | Sent to every provider.                                                                                                                                                                |
| `frequency_penalty`, `presence_penalty` | Sent to OpenAI, OpenRouter and custom models.                                                                                                                                          |
| `chatbotId`                             | The ID of a saved chatbot. Its instruction, with the fixed chatbot guidelines and its variables filled in, replaces the system message, and its model is used when `model` is missing. |

### Model

`model` is the provider, a hyphen and the provider's model identifier:

| Provider     | Example                                            |
| ------------ | -------------------------------------------------- |
| OpenAI       | `openai-gpt-4.1-nano`                              |
| Anthropic    | `anthropic-claude-sonnet-4-5`                      |
| Google       | `google-gemini-2.5-flash`                          |
| OpenRouter   | `openrouter-openai/gpt-4.1`                        |
| Custom model | `custom-` followed by the name the model was given |

SleekAI turns every underscore into a hyphen before it splits the string, so
`openai_gpt-4.1-nano` works too. `wp sleek ai models list` prints the model
identifiers available with the stored keys.

[Custom models](/docs/ai/settings/custom-models.md) belong to the user who
created them, and SleekAI finds one only while that user is the current user.
Cron jobs, visitor requests and WP-CLI calls without `--user` therefore need a
provider model.

### Return value

The function returns an array. On success it holds the reply text in
`content`; on failure it holds a message in `error`, for example
`API key not found for provider: openai`, or `HTTP 401: ` followed by the
provider's response.

```php
$success = [
    'success' => true,
    'content' => '1 + 1 equals 2.',
];

$failure = [
    'success' => false,
    'error' => 'API key not found for provider: openai',
];
```

### Providers

The same `messages` work for every provider: SleekAI converts them to each
provider's format. The request does not stream and blocks until the provider
answers, so run it in a background task or an admin action rather than on
every page view.

## Render a chatbot

`sleekAi_render_chatbot()` prints a chatbot built from an array. See the
[Chatbot API](/docs/ai/chatbots/api.md).

## Other functions

- `sleekAi()` returns the `SleekAi\Plugin` instance, which has `getVersion()`,
  `getPluginDir()`, `getPluginFile()` and `getPluginUrl()`.
- `sleekAi_use_polling()` returns whether replies are polled rather than
  streamed, after the `sleekAi/polling` filter; see
  [Streaming](/docs/ai/streaming.md).

```php
if (function_exists('sleekAi')) {
    $version = sleekAi()->getVersion();
    $polling = sleekAi_use_polling();
}
```

To run code once SleekAI has booted, use the `sleekAi/init` action, which
passes the same instance. SleekAI boots on `plugins_loaded`, so add the
callback from a plugin; a theme loads too late to catch it.
