# Server Actors

Copy for LLM

A [Server Actor](https://docs.apify.com/platform/actors/running/standby) (formerly Actor Standby) runs a web server. You call it over HTTP, and the platform keeps a run ready to answer, so each call skips the run startup.

Formerly Actor Standby

Actor Standby is the former name of Server Actors, and it'll be deprecated. The platform documentation and the API still use it, for example in the Actor's `standbyUrl` and `actorStandby` fields. In the client, `ActorClient.standby()` is a deprecated alias of `ActorClient.server()` that will be removed in v4.

To work with a Server Actor, get its server client with [`ActorClient.server()`](https://docs.apify.com/api/client/js/reference/class/ActorClient.md#server). To send a request, use [`ActorServerClient.request()`](https://docs.apify.com/api/client/js/reference/class/ActorServerClient.md#request). It takes the path on the Actor's server, and optionally the HTTP method, query parameters, headers, and a body:

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });

const actorServerClient = client.actor('username/actor-name').server();



// Send a GET request to the /search endpoint of the Actor's server.

// The method returns error responses too, so check the status first.

const searchResponse = await actorServerClient.request('/search', { params: { q: 'apify' } });

if (searchResponse.status !== 200) throw new Error(`Search failed with status ${searchResponse.status}`);

console.log(searchResponse.data);



// Send a POST request with a JSON body.

const itemsResponse = await actorServerClient.request('/items', {

    method: 'POST',

    data: { url: 'https://apify.com' },

});

console.log(itemsResponse.status);
```

The method resolves to the server's response, with the body parsed by its content type in `data`. To get the body as raw bytes, pass `responseType: 'buffer'`. Pass `responseType: 'stream'` to get it unread, as a web `ReadableStream`. To find out which endpoints an Actor serves, see its page in [Apify Store](https://apify.com/store), its README, or its OpenAPI specification. For details, see [Server URL and OpenAPI specification](#server-url-and-openapi-specification).

## How the request is sent[](#how-the-request-is-sent)

Note that:

* The client takes the server's URL from the Actor's `standbyUrl`. It fetches the Actor from the API on the first call and reuses the URL for later calls through the same `ActorClient`. If the Actor isn't a Server Actor, the server client throws an `ApifyClientError`.
* The path is always resolved against that URL, so a path can't send the request to another host.
* By default, the request body is sent uncompressed, since the Actor's server may not decode a compressed body. If the server decodes it, pass `compression`, for example `compression: 'gzip'`. The client then compresses large bodies the same way as for API calls.

## Server URL and OpenAPI specification[](#server-url-and-openapi-specification)

The Actor server client also gives you the server's URL and its OpenAPI specification:

* [`ActorServerClient.getUrl()`](https://docs.apify.com/api/client/js/reference/class/ActorServerClient.md#getUrl) returns the server's URL. It fetches and reuses the URL the same way as `request()`.
* [`ActorServerClient.getOpenApiSpec()`](https://docs.apify.com/api/client/js/reference/class/ActorServerClient.md#getOpenApiSpec) returns the server's OpenAPI specification as an object. It's the [web server schema](https://docs.apify.com/platform/actors/development/actor-definition/web-server-schema) from the Actor's definition, taken from the build that serves the server interface. That's the build set in the Actor's `actorStandby.build` field, or `latest` if none is set. If the Actor doesn't define a web server schema, the method returns `undefined`.

The following example prints the server's URL and the paths of its endpoints:

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });

const actorServerClient = client.actor('username/actor-name').server();



// Get the URL of the Actor's server.

const serverUrl = await actorServerClient.getUrl();

console.log(serverUrl);



// Get the OpenAPI specification and list the server's endpoints.

const spec = await actorServerClient.getOpenApiSpec();

if (spec) {

    console.log(Object.keys(spec.paths as Record<string, unknown>));

}
```

## Authentication[](#authentication)

The request carries the client's API token in the `X-Apify-Authorization` header. The platform removes the token before it forwards the request, so the token doesn't reach the Actor's code. The method doesn't follow redirects and returns a redirect response as it is, since following it would send the token to a host the Actor's code picked. In a browser, the redirect response can't be read, so the request fails with an `ApifyClientError` instead.

The headers you pass in `headers` reach the Actor's server as they are. If the Actor reads the `Authorization` header for its own authentication, pass that header in `headers`.

For details, see [How do I authenticate my requests](https://docs.apify.com/platform/actors/running/standby#how-do-i-authenticate-my-requests) in the platform documentation.

## Calling a Server Actor from a Server Actor[](#calling-a-server-actor-from-a-server-actor)

A multi-tenant Server Actor shares the developer's runs among its users, so the platform tells requests apart by a new ID it sends in the `X-Actor-Request-ID` header of each request it forwards to the Actor. The Actor uses that ID to charge the request's caller, and to pass the caller's pricing tier on to Actors it calls.

When your multi-tenant Server Actor calls another one while handling a request, pass the incoming `X-Actor-Request-ID` header in `parentRequestId`. The client sends it as the same header, so the called Actor charges at the pricing tier of your incoming request. Use a client created with the run's `APIFY_TOKEN`. For details, see [Compose multi-tenant Server Actors](https://docs.apify.com/platform/actors/development/programming-interface/standby#compose-server-actors).

```
const response = await client.actor('username/other-actor').server().request('/search', {

    params: { q: 'apify' },

    parentRequestId: req.headers['x-actor-request-id'],

});
```

## Timeouts, retries, and errors[](#timeouts-retries-and-errors)

When no server run is ready, the first request waits for the platform to start one. The method uses the `long` timeout tier by default for that reason. You can pass a different `timeoutSecs`, or a `signal` to abort the request, the same way as for API calls. For details, see [Timeouts](https://docs.apify.com/api/client/js/docs/concepts/timeouts.md).

The method returns the server's response whatever its status, so check `status` before you use `data`. Only rate-limit (429) and Service Unavailable (503) responses are retried, with the client's retry settings. Once the retries run out, the method returns the last response. The platform sends these when it can't pass the request to a server run, but the Actor's code can return them too, and then a retry sends the request to the Actor again. Network errors and timeouts aren't retried, since the Actor may have already handled the request. For details, see [Retries](https://docs.apify.com/api/client/js/docs/concepts/retries.md).

The method throws only when it doesn't get a response from the server: an `ApifyApiError` when the API can't return the Actor, for example because it doesn't exist, an `ApifyClientError` when it isn't a Server Actor, or the transport error when the request fails or times out. For details, see [Error handling](https://docs.apify.com/api/client/js/docs/concepts/error-handling.md).

With the default `responseType: 'parsed'`, a body that doesn't parse as its `Content-Type` says is handled by status. A success response throws an `InvalidResponseBodyError`. An error response is returned with the raw bytes in `data`, so you still get its status. If the server may send such bodies, pass `responseType: 'buffer'` and parse the body yourself.
