# Authenticate an automation

AI agent getting started? Read [Agents: start here](https://docs.openeng.io/agents/start-here.md) first.

Give a backend job its own scoped access.

Source: https://docs.openeng.io/developers/service-identities/

A service identity lets an automation use explicitly assigned permissions. It does not act as each visitor to your app. Install the [SDK](https://docs.openeng.io/developers/overview.md#install-the-sdk) and keep its credential on your backend.

## Authenticate a backend integration

For a backend job or an internal app with deliberately shared application-level access, use a service identity.

An administrator creates the identity, grants it the project or organisation capabilities it needs, and issues a credential. A new service identity belongs to its organisation at **No role**, which grants no capability. Keep the issued secret in your backend's secret store.

### Create and scope the identity

1. In the intended organisation, create a service identity with
   `openeng admin-sdk createServiceIdentity --input '{"name":"nightly import"}'`.
2. Grant only the project roles the integration needs, or change its existing organisation role for access across projects. Prefer project scope for a single-project integration.
3. Issue a credential. The CLI has no command for it, because it leaves out every route that returns a credential. In the web app, open **Admin**, choose **Service identities**, find the identity and choose **Issue credential**. In code, call the Admin SDK's `issueServiceCredential` with `{ principalId: "<principal-id>" }`. Keep the returned secret in your backend's secret store. See the
   [CLI reference](https://docs.openeng.io/reference/cli.md) for the command forms that do exist.
4. Verify that the identity can perform the intended work and that other operations are refused.

Credential issuance and membership grants are separate actions. Issuing a credential does not add data access. [Access and permissions](https://docs.openeng.io/concepts/access.md) explains membership scope.

The backend exchanges that credential for platform access as the service identity. Keep the credential and resulting session on your backend. Your backend's authenticated routes decide who may use this application-level access; the service identity does not represent each individual person visiting your app. Revoke credentials that are no longer needed.

Exchange the credential on the backend, then use the returned access token with the Objects
client:

```ts
import {
  createHttpsTransport,
  createObjectsTransportClient,
  serviceCredential,
  toCredential,
} from "@openengio/sdk/objects";

const origin = "https://api.openeng.io";
const transport = createHttpsTransport(origin);
const session = await serviceCredential(transport, origin, {
  input: {
    credentialId: "credential-id",
    secret: toCredential("credential-secret"),
  },
});
if (!("refusal" in session) && session.replayed !== true) {
  const objects = createObjectsTransportClient(transport, origin, session.result.accessToken);
  const answer = await objects.readObject({
    selector: { projectId: "project-1", ref: "main" },
    input: { objectId: "object-1" },
  });
  console.log(answer);
}
```
