---
title: set_user_id (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/setuserid-python-agent-api
---

## Syntax [#syntax]

```py
newrelic.agent.set_user_id(user_id)
```

A standardized way to identify an end user on `Transaction` events with the `set_user_id` method.

This method is used for user tracking, which is a standardized way to identify an end user on `Transaction` events.

## Requirements [#requirements]

Python agent version 8.8.0 or higher.

## Description [#description]

This call is used within the context of a transaction to attach an end user to a particular transaction or error event. This allows the customer to gain insight about a particular end user.

## Parameters [#parameters]

| Parameter          | Description |
| ------------------ | ----------- |
| `user_id` _string_ | Required.   |

## Return values [#return-values]

`None`.

When successful, the API will add the user ID as an agent attribute.

When unsuccessful, the API will not add `enduser.id` as an agent attribute.  A failure may occur for several reasons:

-   The current transaction is not enabled.
-   Nothing was provided as an input to the API.
-   Something was provided but it was not a string.

## Example usage [#examples]

### Set user ID inside a background task [#set-user-id-example]

An example of using `set_user_id` inside a simple Flask app to set the user id of the transaction:

```py
from flask import Flask
import newrelic.agent

app = Flask("Flask Test App")

@app.route("/")
def hello():
    try:
        newrelic.agent.set_user_id("my-user-id")
        raise ValueError("Oh no!")
    except:
        newrelic.agent.notice_error()
    return "Hello World!"

if __name__ == '__main__':
    app.run()
```

![Example app with set_user_id with Flask](https://docs.newrelic.com/images/errors-inbox_screenshot-crop_python-set-user-id.webp "Example app with set_user_id with Flask")
