> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Tracing to Flask applications

> Add tracing instrumentation to Flask applications using the Traceloop SDK for request observability.

This guide demonstrates how to use TrueFoundry OtelCollector along with the Traceloop SDK to instrument FastAPI applications.
In this example, we'll create a simple Flask application with a `/greet` endpoint that demonstrates how to integrate TrueFoundry's tracing capabilities.

<Steps>
  <Step title="Create Tracing Project, API Key and copy tracing code">
    Follow the instructions in [Getting Started](/docs/tracing/tracing-getting-started) to create a tracing project, generate API key and copy the
    tracing code.
  </Step>

  <Step title="Install Dependencies">
    First, you need to install the following

    ```shell lines theme={"dark"}
    pip install dotenv flask opentelemetry_instrumentation_flask traceloop-sdk
    ```
  </Step>

  <Step title="Add Tracing code to Flask application">
    For FastAPI applications, we need to add the `Traceloop.init()` call to the application and instrument the FastAPI app with OpenTelemetry.

    ```python FastAPI Code {6-9,13-22,26} lines theme={"dark"}
    import random
    import os
    from dotenv import load_dotenv
    from flask import Flask

    from opentelemetry.instrumentation.flask import FlaskInstrumentor

    # importing traceloop sdk
    from traceloop.sdk import Traceloop

    load_dotenv()

    # Add the traceloop init code to your application
    TFY_API_KEY = os.environ.get("TFY_API_KEY")
    Traceloop.init(
        api_endpoint="<enter_your_api_endpoint>",
        app_name="your tracing application name",
        headers = {
            "Authorization": f"Bearer {TFY_API_KEY}",
            "TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
        },
    )

    app = Flask(__name__)

    FlaskInstrumentor().instrument_app(app)

    def get_random_greeting():
        greetings = ["Hello", "Hi", "Hey", "Good morning", "Good afternoon", "Good evening"]
        return random.choice(greetings)

    @app.get("/greet")
    def root():
        greeting = get_random_greeting()
        return {"message": f"{greeting}, wishing you a great day!"}


    if __name__ == "__main__":
        app.run(debug=True)
    ```
  </Step>

  <Step title="Run your application and view logged trace">
    <Frame caption="">
      <img src="https://mintcdn.com/truefoundry/2MMcllD7kMlpnaWX/images/tracing_flask.png?fit=max&auto=format&n=2MMcllD7kMlpnaWX&q=85&s=7db204d72d8d9cbb61070e8c4c70c1b8" width="3024" height="1716" data-path="images/tracing_flask.png" />
    </Frame>
  </Step>
</Steps>
