# Sign in from your server

Use your registered signing key to get a token. Your server sends that token with each payment link request.

## Prove which business is calling

1. Your server sends its identity ID and public address to Cadmos.
2. Cadmos returns a message that expires in two minutes. Check the domain, address, identity and audience, then sign the original message with your private key.
3. Your server exchanges the signature for an access token valid for 15 minutes.

### Show the sign-in code · Node.js and ethers 6

Save this as `sign-in.mjs` on your server. It reads the two environment variables from [account setup](https://cadmospay.com/developers/guides/access.md).

```javascript
import { Wallet } from 'ethers';

const key = new Wallet(process.env.CADMOS_SIGNING_KEY);
const identityId = process.env.CADMOS_IDENTITY_ID.toLowerCase();
const auth = 'https://auth.cadmos.finance/v1/external-auth';

async function post(path, body) {
  const response = await fetch(auth + path, {
    method: 'POST', redirect: 'error',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body),
  });
  if (!response.ok) throw new Error(path + ' returned ' + response.status);
  return response.json();
}

const challenge = await post('/challenge', { identityId, address: key.address });
const { challengeId, message, expiresAt } = challenge;
const lines = message.split('\n');
if (lines[0] !== 'auth.cadmos.finance wants you to sign in with your Ethereum account:' ||
    lines[1] !== key.address ||
    lines[3] !== 'Authenticate external identity ' + identityId + ' for cadmos-pay-be.' ||
    lines[5] !== 'URI: ' + auth ||
    !lines.includes('Request ID: ' + challengeId) ||
    !(Date.parse(expiresAt) > Date.now())) {
  throw new Error('Unexpected or expired Cadmos challenge');
}

const signature = await key.signMessage(message);
const { accessToken } = await post('/token', { challengeId, message, signature });
process.stdout.write(accessToken);
```

## Use the token for API calls

```bash
CADMOS_ACCESS_TOKEN=$(node sign-in.mjs) && export CADMOS_ACCESS_TOKEN
```

Send it as `Authorization: Bearer <accessToken>` only to `api.cadmospay.com`. Repeat sign-in when it expires.

[Create your first payment link](https://cadmospay.com/developers/guides/create-link.md).
