Keep the external request in a PHP service. Your endpoint and widget can then share one implementation.
For a Widget Builder integration, follow the Data Source walkthrough. Vanilla already provides server-side secret headers and request proxying for that approach.
Provision the credential
The Code Deploy example reads MyCustomCatalog.ApiKey from server configuration. This is a tutorial key, not a built-in setting.
Provision its value through the server configuration process agreed for your environment. Do not commit the credential to the partner repository or return it in widget props.
A configuration value is not automatically an encrypted secret. Data Sources have their own encrypted secret-header storage. Confirm how your Code Deploy environment provisions and protects configuration values.
The examples below use an API key header. They do not implement OAuth authorization, token refresh or per-member credentials.
Create an HTTP client
Create Library/CatalogHttpClient.php.
This abbreviated class uses a fixed placeholder host. Replace that host with your actual API. Supply the imports shown here.
<?php
namespace MyCustom\Catalog\Library;
use Garden\Http\HttpClient;
use Garden\Web\Exception\ServerException;
use Vanilla\Contracts\ConfigurationInterface;
use Vanilla\Web\SafeCurlHttpHandler;
class CatalogHttpClient extends HttpClient
{
public function __construct(ConfigurationInterface $config)
{
parent::__construct("https://api.example.com");
$key = (string) $config->get("MyCustomCatalog.ApiKey", "");
if ($key === "") {
throw new ServerException("The catalog is not configured.");
}
$this->setHandler(new SafeCurlHttpHandler());
$this->setDefaultHeader("Accept", "application/json");
$this->setDefaultHeader("X-API-Key", $key);
$this->setDefaultOption("timeout", 5);
$this->setThrowExceptions(true);
}
}
The five-second timeout is an example budget. Choose one that fits your page and upstream service.
SafeCurlHttpHandler is the handler used by Vanilla's Data Source client. It validates destinations to protect internal services. Use a reachable public API for local integration testing. A service on localhost is not a substitute for that destination.
Keep the base URL under server control. Member input should select validated query parameters, not arbitrary hosts.
Fetch and cache a validated page
Create Library/CatalogService.php.
This abbreviated implementation uses the schemas from the previous chapter. It assumes the external service returns the tutorial's page shape.
<?php
namespace MyCustom\Catalog\Library;
use Garden\Web\Exception\ServerException;
use Vanilla\Models\ModelCache;
class CatalogService
{
private ModelCache $cache;
public function __construct(
private CatalogHttpClient $http,
\Gdn_Cache $cache
) {
$this->cache = new ModelCache("myCustomCatalog", $cache);
}
public function fetchPage(array $params): array
{
$params = CatalogSchema::query()->validate($params);
return $this->cache->getCachedOrHydrate(
[$params],
function (array $query): array {
try {
$response = $this->http->get("/catalog", $query);
return CatalogSchema::page()->validate($response->getBody());
} catch (\Throwable $error) {
// Add a sanitized diagnostic through your logging policy.
throw new ServerException("The catalog is unavailable.", 502);
}
},
cacheOptions: [ModelCache::OPT_TTL => 120]
);
}
}
The cache key includes the query, sort, page and limit. The two-minute TTL is a tutorial choice. Cache only successfully validated responses. An upstream failure must not become an empty successful result.
The cache callback receives the arguments supplied to getCachedOrHydrate(). Here it receives the query array.
This example assumes the same upstream catalog is available to every authorized caller. If the response varies by account or permission context, include that context in the cache key and enforce access before reading it. Never share private results through a public cache entry.
Add URL-scheme and host validation to the response adapter when your integration accepts external links. Use a mapping step if the provider's payload differs from the tutorial contract.
Connect the endpoint
Update MyCustomCatalogApiController to receive the service:
// Class excerpt. Keep the namespace and Data import from chapter 2.
use MyCustom\Catalog\Library\CatalogService;
use MyCustom\Catalog\Library\CatalogSchema;
public function __construct(private CatalogService $catalog)
{
}
public function index(array $query = []): Data
{
$this->permission("session.valid");
$query = CatalogSchema::query()->validate($query);
return new Data($this->catalog->fetchPage($query));
}
The controller checks the caller. The service validates the parameters again so PHP callers cannot bypass that validation.
Handle provider limits
A cache reduces repeated calls for the same query. It does not enforce a global request quota.
Keep the page size bounded. Submit search changes deliberately, or debounce them when you implement live search. Avoid automatic retry loops when the provider returns 429 or is unavailable.
If the provider supplies Retry-After, account for it in your retry policy. Add backoff or a wider integration limit when the provider contract requires it. Those are application behaviors to implement; ModelCache does not supply them automatically.
Check the result
Replace the placeholder host with an actual compatible service for a manual check. Request two different queries and confirm that the upstream receives different parameters.
Also stub a timeout, a 429 response and a malformed body. Each must reach the widget's error state. None should appear as “no results.”
Checkpoint: the local endpoint returns a validated catalog page while credentials stay on the server. Continue with Create a configurable widget.