Install the SDK

PHP

What each SDK needs, how to install its package, how to call Slen without it, and the API key it calls with.

3 min read

Slen has an SDK for PHP, the package slen/sdk, and one for Node.js, the package @slen/sdk. Both call the same API. This page says what each one needs, how to install its package, and which API key it calls with.

What the SDK needs

  • PHP 8.3 or later, with ext-json.

  • psr/http-client 1 and psr/http-factory 1 — the interfaces of the HTTP client the SDK sends its requests with.

  • psr/http-message 1.1 or 2 — the interfaces of a request and of an answer.

  • php-http/discovery 1.19 or a later 1 — finds the HTTP client and the factories installed in your application.

  • psr/log 1.1, 2 or 3 — the interface of the logger the SDK writes its journal to.

The package requires no HTTP client of its own: your application brings one. These packages are used only when your application has them.

  • guzzlehttp/guzzle 7.9 or a later 7, or 8.3 or a later 8 — an HTTP client the SDK builds itself, with the timeouts of its config.

  • symfony/http-client 7 — the same, with Symfony HttpClient.

  • nyholm/psr7 1.8 or a later 1 — the factories of requests Symfony HttpClient is used with.

  • illuminate/support of Laravel 11 or 12 — the Laravel integration: the service provider, the Slen facade and the config file.

The Laravel integration is for Laravel 11 and 12: beside an earlier illuminate/support the package does not install.

Any other PSR-18 client works too. It keeps its own timeouts — Configuration says how the SDK treats it.

Install the package

Composer
composer require slen/sdk guzzlehttp/guzzle

Every call of the SDK can be sent without it: the SDK sends ordinary HTTP requests to the API. This is the request of the Quickstart, over HTTP:

Terminal
curl https://api.slen.ai/api/v1/operations/execute \
  -H "Authorization: Bearer $SLEN_API_KEY" \
  -H "Idempotency-Key: order-42:email" \
  -H "Content-Type: application/json" \
  -d '{
    "operation": "generate_email",
    "input": {
      "messages": [
        {
          "role": "user",
          "content": "Write a short follow-up email."
        }
      ]
    }
  }'
200 OK
{
  "data": {
    "execution_id": "74438200973594700",
    "status": "completed",
    "operation": "generate_email",
    "output": {
      "model": "gpt-4o",
      "content": "Hi Ada, just checking in on the proposal I sent last week.",
      "finish_reason": "stop",
      "tool_calls": null,
      "usage": {
        "input_tokens": 14,
        "output_tokens": 16,
        "cached_input_tokens": 0
      }
    },
    "error": null,
    "error_code": null,
    "error_message": null,
    "cost": {
      "estimated": "0.00412500",
      "actual": null
    },
    "cache": {
      "hit": false,
      "cached_at": null
    },
    "replayed": false,
    "waiting_on": null,
    "trace_url": "/v1/executions/74438200973594700"
  }
}

Overview states the conventions every request of the API follows.

The API key

Every call of the SDK carries an API key of your project. Issue one on Keys & Access — Manage API keys walks through it. A key is shown once, when it is issued.

What a key may call depends on its scopes. A run of a flow needs gateway:write, a read of customers customers:read, and a change of customers or their credits customers:write. Client and methods names the scope of every call, and Authentication says what each scope includes.

Keep the key where your application keeps its secrets, and out of code that runs in a browser. The examples of this reference read it from the SLEN_API_KEY environment variable.

Terminal
export SLEN_API_KEY="sl_live_…"

The PHP SDK is given the key when it is built. In a Laravel application the config file of the SDK reads the variable.

Where to go next

  • Quickstart — the first run of a flow, step by step.

  • Configuration — the options of the SDK and what each is for.

  • Laravel integration — the SDK of a Laravel application, from its container or the Slen facade.