# Containers

Before using any **container methods** (`add_item`, `remove_item`, `has_item`, etc.), you must first retrieve the container object using the following export:

```lua
local container = exports.list_inventory:get_container(id)
```

This returns the active container object for the given `id`, complete with all public methods.\
Then you can use the methods below on the container like so:

```lua
local container = exports.list_inventory:get_container("some_container_id")

if not container then 
    print("No container found for that ID")
    return 
end

container:add_item("bandage", 2)
```

***

## get\_data

Get a specific field or the full container data.

#### Params

* `key` (string, optional): The key to fetch

#### Returns

* Value of the key or full data table

```lua
local coords = container:get_data("coords")
```

***

## set\_data

Set a custom field on the container.

#### Params

* `key` (string): Field name
* `value` (any): Value to store

#### Returns

* `true`

```lua
container:set_data("custom_label", "Loot Crate")
```

***

## get\_items

Returns a table of all items in the container.

#### Returns

* `table`: All container items indexed by slot

```lua
local items = container:get_items()
```

***

## get\_item

Returns the first matching item by slot, ID, or metadata.

#### Params

* `lookup` (number|string|table): Slot number, item ID, or metadata table

#### Returns

* Item object or `nil`

```lua
local item = container:get_item("bandage")
```

***

## has\_item

Checks if container contains an item by slot, ID, or metadata.

#### Params

* `lookup` (number|string|table): Slot number, item ID, or metadata table
* `amount` (number): Amount required (default: 1)

#### Returns

* `true` if found, otherwise `false`

```lua
if container:has_item("weapon_pistol", 1) then ... end
```

***

## add\_item

Adds an item to the container.

#### Params

* `id` (string): Item ID
* `amount` (number): Quantity
* `item_data` (table, optional): Item metadata (e.g., quality, custom props)

#### Returns

* `true` on success, or `false, reason` on failure

```lua
container:add_item("ammo_pistol", 24)
```

***

## remove\_item

Removes item(s) by slot, ID, or metadata.

#### Params

* `lookup` (number|string|table): Slot number, item ID, or metadata table
* `amount` (number): Amount to remove

#### Returns

* `true` on success, `false` otherwise

```lua
container:remove_item("bandage", 2)
```

***

## split\_item

Splits a stack in one slot into a new slot.

#### Params

* `slot` (number|string): Source slot
* `amount` (number): Amount to split

#### Returns

* `true` on success, `false` otherwise

```lua
container:split_item(1, 5)
```

***

## clear\_items

Removes all items from the container.

#### Returns

* `true`

```lua
container:clear_items()
```

***

## save

Persists the container to the database (if marked as persistent).

#### Returns

* `true` on success, `false` on failure

```lua
container:save()
```

***

## sync

Sends container inventory state to all clients.

```lua
container:sync()
```
