# Origins

An origin is the server an application retrieves content from. These `azion/applications` functions create, list, read, update, and delete the origins of an application. The API identifies an origin by its `origin_key`, a UUID, which every function except `createOrigin` and `getOrigins` takes as `originKey`.

---

## createOrigin

Creates an origin in an application. An origin is the server the application retrieves content from.

```typescript
function createOrigin(params: {
  applicationId: number;
  data: ApiCreateOriginPayload;
  options?: AzionClientOptions;
}): Promise<AzionApplicationResponse<AzionOrigin>>;
```

| Parameter       | Type                                                                                               | Required | Description                                                                                        |
| --------------- | -------------------------------------------------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------- |
| `applicationId` | `number`                                                                                           | Yes      | The ID of the application.                                                                         |
| `data`          | [`ApiCreateOriginPayload`](/en/documentation/devtools/azion-lib/application/types/#azionorigin)    | Yes      | The settings of the origin. The sample sets `name`, `origin_type`, `addresses`, and `host_header`. |
| `options`       | [`AzionClientOptions`](/en/documentation/devtools/azion-lib/application/types/#azionclientoptions) | No       | Request options.                                                                                   |

Returns `data` as the created [AzionOrigin](/en/documentation/devtools/azion-lib/application/types/#azionorigin). The API identifies an origin by its `origin_key`, a UUID, which the other origin functions take as `originKey`.

This sample is JavaScript, because `ApiCreateOriginPayload` requires every origin field, including `origin_key` and the HMAC fields, and types `origin_type` as an enum:

```javascript
import { createOrigin } from 'azion/applications';

const applicationId = 1234567890; // Replace with the actual application ID

// Define the data for the new origin
const newOriginData = {
  name: 'my-origin',
  origin_type: 'single_origin',
  addresses: [{ address: 'example.com' }],
  host_header: '${host}',
};

// Create a new origin
const { data: newOrigin, error } = await createOrigin({ applicationId, data: newOriginData });

if (error) {
  console.error('Error creating origin:', error);
} else {
  console.log('Created origin:', newOrigin.origin_key, newOrigin.name, newOrigin.addresses);
}
```

Output:

```text
Created origin: 11111111-1111-1111-1111-111111111111 my-origin [
  {
    address: 'example.com',
    weight: null,
    server_role: 'primary',
    is_active: true
  }
]
```

---

## getOrigins

Lists the origins of an application, one page at a time.

```typescript
function getOrigins(params: {
  applicationId: number;
  params?: ApiListOriginsParams;
  options?: AzionClientOptions;
}): Promise<AzionApplicationCollectionResponse<AzionOrigin>>;
```

| Parameter       | Type                                                                                               | Required | Description                         |
| --------------- | -------------------------------------------------------------------------------------------------- | -------- | ----------------------------------- |
| `applicationId` | `number`                                                                                           | Yes      | The ID of the application.          |
| `params`        | [`ApiListOriginsParams`](/en/documentation/devtools/azion-lib/application/types/#list-parameters)  | No       | Pagination: `page` and `page_size`. |
| `options`       | [`AzionClientOptions`](/en/documentation/devtools/azion-lib/application/types/#azionclientoptions) | No       | Request options.                    |

Returns an [AzionApplicationCollectionResponse](/en/documentation/devtools/azion-lib/application/types/#azionapplicationcollectionresponse) whose `data.results` holds [AzionOrigin](/en/documentation/devtools/azion-lib/application/types/#azionorigin) objects.

```typescript
import { getOrigins } from 'azion/applications';

const applicationId = 1234567890; // Replace with the actual application ID

const { data: origins, error } = await getOrigins({ applicationId, params: { page: 1, page_size: 10 } });

if (error) {
  console.error('Error listing origins:', error);
} else {
  console.log(`Found ${origins?.count} origins:`, origins?.results.map((o) => `${o.name} (${o.origin_key})`));
}
```

Output:

```text
Found 1 origins: [ 'my-origin (11111111-1111-1111-1111-111111111111)' ]
```

---

## getOrigin

Returns one origin of an application by its origin key.

```typescript
function getOrigin(params: {
  applicationId: number;
  originKey: string;
  options?: AzionClientOptions;
}): Promise<AzionApplicationResponse<AzionOrigin>>;
```

| Parameter       | Type                                                                                               | Required | Description                             |
| --------------- | -------------------------------------------------------------------------------------------------- | -------- | --------------------------------------- |
| `applicationId` | `number`                                                                                           | Yes      | The ID of the application.              |
| `originKey`     | `string`                                                                                           | Yes      | The `origin_key` of the origin, a UUID. |
| `options`       | [`AzionClientOptions`](/en/documentation/devtools/azion-lib/application/types/#azionclientoptions) | No       | Request options.                        |

Returns `data` as an [AzionOrigin](/en/documentation/devtools/azion-lib/application/types/#azionorigin). A key that matches no origin returns `error` with the message `HTTP error! Status: 404 - NOT FOUND` and the operation `get origin`.

```typescript
import { getOrigin } from 'azion/applications';

const applicationId = 1234567890; // Replace with the actual application ID
const originKey = '11111111-1111-1111-1111-111111111111'; // Replace with the origin key (a UUID)

const { data: origin, error } = await getOrigin({ applicationId, originKey });

if (error) {
  console.error('Error retrieving origin:', error);
} else {
  console.log('Origin details:', origin?.origin_key, origin?.name, origin?.origin_type, origin?.host_header);
}
```

Output:

```text
Origin details: 11111111-1111-1111-1111-111111111111 my-origin single_origin ${host}
```

---

## updateOrigin

Sends the origin fields you pass in a `PATCH` request.

```typescript
function updateOrigin(params: {
  applicationId: number;
  originKey: string;
  data: ApiUpdateOriginRequest;
  options?: AzionClientOptions;
}): Promise<AzionApplicationResponse<AzionOrigin>>;
```

| Parameter       | Type                                                                                               | Required | Description                               |
| --------------- | -------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------- |
| `applicationId` | `number`                                                                                           | Yes      | The ID of the application.                |
| `originKey`     | `string`                                                                                           | Yes      | The `origin_key` of the origin to update. |
| `data`          | [`ApiUpdateOriginRequest`](/en/documentation/devtools/azion-lib/application/types/#azionorigin)    | Yes      | The fields to change.                     |
| `options`       | [`AzionClientOptions`](/en/documentation/devtools/azion-lib/application/types/#azionclientoptions) | No       | Request options.                          |

Returns `data` as the updated [AzionOrigin](/en/documentation/devtools/azion-lib/application/types/#azionorigin).

This sample is JavaScript, because `ApiUpdateOriginRequest` requires an `id`, which the request does not need:

```javascript
import { updateOrigin } from 'azion/applications';

const applicationId = 1234567890; // Replace with the actual application ID
const originKey = '11111111-1111-1111-1111-111111111111'; // Replace with the origin key (a UUID)

const { data: updatedOrigin, error } = await updateOrigin({
  applicationId,
  originKey,
  data: { name: 'my-origin-updated' },
});

if (error) {
  console.error('Error updating origin:', error);
} else {
  console.log('Updated origin:', updatedOrigin.origin_key, updatedOrigin.name);
}
```

Output:

```text
Updated origin: 11111111-1111-1111-1111-111111111111 my-origin-updated
```

---

## deleteOrigin

Deletes an origin of an application by its origin key.

```typescript
function deleteOrigin(params: {
  applicationId: number;
  originKey: string;
  options?: AzionClientOptions;
}): Promise<AzionApplicationResponse<void>>;
```

| Parameter       | Type                                                                                               | Required | Description                               |
| --------------- | -------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------- |
| `applicationId` | `number`                                                                                           | Yes      | The ID of the application.                |
| `originKey`     | `string`                                                                                           | Yes      | The `origin_key` of the origin to delete. |
| `options`       | [`AzionClientOptions`](/en/documentation/devtools/azion-lib/application/types/#azionclientoptions) | No       | Request options.                          |

A successful delete returns `error` with the message `Expected JSON response, but got: `, so the sample reads the origin back with [getOrigin](#getorigin). A `404` error confirms the delete.

```typescript
import { deleteOrigin, getOrigin } from 'azion/applications';

const applicationId = 1234567890; // Replace with the actual application ID
const originKey = '11111111-1111-1111-1111-111111111111'; // Replace with the origin key (a UUID)

const { error: deleteError } = await deleteOrigin({ applicationId, originKey });

// On success (HTTP 204) the envelope still carries an error, so confirm by reading the origin back
const { error } = await getOrigin({ applicationId, originKey });
if (error) {
  console.log(`Origin ${originKey} deleted (read back: ${error.message})`);
} else {
  console.error('Error deleting origin:', deleteError);
}
```

Output:

```text
Origin 11111111-1111-1111-1111-111111111111 deleted (read back: HTTP error! Status: 404 - NOT FOUND)
```

---

## Related resources

- [Azion Lib](/en/documentation/devtools/azion-lib.md): The Azion Lib modules and the npm package each one ships in.
- [Applications](/en/documentation/devtools/azion-lib/application.md): The module's authentication, its response envelope, and the errors its functions report.
- [Application types](/en/documentation/devtools/azion-lib/application/types.md): The payload and response types these functions take and return.
- [Origins](/en/documentation/platform/connectors/origins.md): The origin an application retrieves content from, and the settings it holds.
