---
title: "Manage UDFs"
description: "Register and manage Lua User-Defined Functions (UDFs) in Aerospike using the Rust client via files or strings."
---

# Manage UDFs

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

The Aerospike Rust client can be used to register User-Defined Functions (UDFs). UDFs are written in the [Lua](https://www.lua.org) programming language that execute in the Aerospike server process. These functions can be grouped together into [Lua packages](https://www.lua.org/pil/15.html) and registered with the Aerospike server cluster. Here is an example Lua package:

```lua
-- Validate value before writing.

function writeWithValidation(r,name,value)

    if (value >= 1 and value <= 10) then

      if not aerospike:exists(r) then

        aerospike:create(r)

      end

      r[name] = value

      aerospike:update(r)

    else

        error("1000:Invalid value")

    end

end

-- Set a particular bin only if record does not already exist.

function writeUnique(r,name,value)

    if not aerospike:exists(r) then

        aerospike:create(r)

        r[name] = value

        aerospike:update(r)

    end

end
```

The UDF Lua package can be created in one of the following formats:

-   ASCII Text File
-   String

## Register UDF in ASCII text file

```rust
match client.register_udf_from_file(&AdminPolicy::default(),

        "/home/user/udf/example.lua", "example.lua", UDFLang::Lua)

        .await {

    Err(err) => println!("Unable to register UDF: {}", err),

    _ => {

        // ... invoke UDF here

    }

}
```

> 📖 **API reference**: [`Client::register_udf_from_file`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.register_udf_from_file) | [`AdminPolicy::default`](https://docs.rs/aerospike/latest/aerospike/struct.AdminPolicy.html) | [`UDFLang`](https://docs.rs/aerospike/latest/aerospike/enum.UDFLang.html)

The UDF Lua package will be registered on all server’s in the cluster. `/home/user/udf/example.lua` is location of the Lua package on the client’s filesystem. `example.lua` is the filename of the Lua package relative to each server’s configured mod-lua user-path in `aerospike.conf`.

```plaintext
mod-lua {

    user-path /opt/aerospike/usr/udf/lua

}
```

In this configuration, the Lua package’s full path on each server’s filesystem will be `/opt/aerospike/usr/udf/lua/example.lua`.

The registration is asynchronous on the server. The server will immediately return an acknowledgement of the registration command and then register the UDF with other servers in the cluster. The client has the option of waiting for the registration task to complete using the returned RegisterTask instance.

## Register UDF in a string

```rust
let code = r#"

-- Validate value before writing.

function writeWithValidation(r,name,value)

    if (value >= 1 and value <= 10) then

      if not aerospike:exists(r) then

        aerospike:create(r)

      end

      r[name] = value

      aerospike:update(r)

    else

        error("1000:Invalid value")

    end

end

-- Set a particular bin only if record does not already exist.

function writeUnique(r,name,value)

    if not aerospike:exists(r) then

        aerospike:create(r)

        r[name] = value

        aerospike:update(r)

    end

end

"#;

match client.register_udf(&AdminPolicy::default(),

        code.as_bytes(), "example.lua", UDFLang::Lua) {

    Err(err) => println!("Unable to register UDF: {}", err),

    _ => {

        // ... invoke UDF here

    }

}
```

> 📖 **API reference**: [`Client::register_udf`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.register_udf) | [`AdminPolicy::default`](https://docs.rs/aerospike/latest/aerospike/struct.AdminPolicy.html) | [`UDFLang`](https://docs.rs/aerospike/latest/aerospike/enum.UDFLang.html)

`example.lua` is the filename of the Lua package relative to each server’s configured mod-lua user-path in `aerospike.conf`.