# Call a Function

URL: /docs/api/workloads/function/invoke

A call needs a credential when the Function's spec.ingress.auth.mode, else its namespace's spec.edgeDefaults.auth.mode, is authenticated; else it needs none.

`POST /function/{name}`

Server: `{gateway}`, funcd's gateway, which serves the calls of Functions. `{gateway}`: The address that server.dataPlaneAddr sets. Its default takes a free port, so set a fixed one, such as 127.0.0.1:8081.

## Authentication

One of:

- none
- `bearer`: A credential of funcd's config, sent as Authorization: Bearer followed by its token.
- `apiKey`: A credential of funcd's config, sent in the X-Api-Key header.

## Parameters

| Parameter | In | Type | Description |
| --- | --- | --- | --- |
| `name` | path | string, required | The Function's name. |
| `X-Funcd-Namespace` | header | string | The Function's namespace, when the Host header names none, as shop.localhost names shop; without either, default. |

## Body

The call's input, any JSON value, which the handler gets as its event's data. A CloudEvent, an object with specversion or data, passes as it is.

`application/json`.

```json
{}
```

## Answers

| Status | Problem type | When | What to do |
| --- | --- | --- | --- |
| 200 |  | The handler's result. |  |
| 400 | `urn:funcd:problem:invalid` | The body did not arrive within the gateway's read timeout. | Send the whole request at once, without pausing. |
| 401 | `urn:funcd:problem:unauthorized` | The namespace's stance asks for a credential, and the call carries none, or one that funcd does not know. | Send a credential of funcd's config as a bearer token in the Authorization header, or in X-Api-Key. |
| 401 | `urn:funcd:problem:unauthorized` | The namespace's stance asks for a credential while server.auth.edge is off, so funcd checks none and refuses the call, whatever credential it carries. | Ask funcd's operator to set server.auth.edge to true. |
| 403 | `urn:funcd:problem:forbidden` | The credential's role or namespaces do not allow it to call Functions in the namespace. | Use a credential of the namespace, such as its developer's. |
| 404 | `urn:funcd:problem:not-found` | The namespace holds no Function of that name that answers at its own address: it does not exist, its ingress is off, or its ingress names a host or a path of its own. | Check the name and the namespace, or call the Function at the address that its ingress names. |
| 413 | `urn:funcd:problem:payload-too-large` | The body is larger than 1 MiB, the most that funcd reads to build the call's event. | Send a smaller body, or pass large data by reference, such as a Bucket key. |
| 422 | `ContractViolation` | The event data does not match the Function's input contract; nothing reached its worker. | Correct the values that details names. |
| 429 | `urn:funcd:problem:resource-exhausted` | The call is over the rate that server.limits sets. | Retry after the seconds that the Retry-After header gives. |
| 500 | `ContractViolation` | The handler's result does not match the Function's output contract, so funcd sends this instead. | Correct the handler, or its output contract. |
| 503 | `urn:funcd:problem:unavailable` | The gateway already serves as many calls at once as server.limits.maxInFlight allows. | Retry the call later. |
| 503 | `urn:funcd:problem:unavailable` | The worker failed during the call, before its answer. funcd does not retry the call. | Retry it, if the handler may run twice. |
| 503 | `urn:funcd:problem:unavailable` | funcd could not read the namespace's settings, so it does not know how to serve the call. | Retry the call later. |
| 503 | `urn:funcd:problem:unavailable` | funcd cannot read the Function's contract. | Retry the call later. |
| 503 | `urn:funcd:problem:unavailable` | The namespace is held for its data, so funcd refuses its calls until the hold is released. | Retry after the seconds that the Retry-After header gives; funcdctl hold release --namespace releases the hold. |
| 503 | `urn:funcd:problem:unavailable` | The namespace is held while a Restore loads it. | Retry after the seconds that the Retry-After header gives, once the Restore has ended. |
| 503 | `urn:funcd:problem:unavailable` | No worker of the Function became ready within invoke.activationTimeout, 30 s by default. | Retry the call; if it keeps failing, read the Function's status for why its worker is not ready. |
| 503 | `urn:funcd:problem:unavailable` | The Function is Failed; the detail names the reason, such as ShapeInvalid. | Correct what the Function's status names, such as its artifact, then retry. |
| 503 | `urn:funcd:problem:unavailable` | funcd is stopping, and the call was waiting for a worker of the Function to become ready. | Retry the call once funcd runs again. |
| 504 | `urn:funcd:problem:deadline-exceeded` | The worker did not start its answer within the call's deadline: the Function's spec.timeout, else invoke.defaultTimeout. | Make the handler answer sooner, or raise its spec.timeout. |