# Invoking deployed Functions | Modal Docs

- **URL:** https://modal.com/docs/guide/trigger-deployed-functions
- **Summary:** Modal lets you take a Function created by a deployment and call it from other contexts.

Copy page

Invoking deployed Functions
===========================

Modal lets you take a Function created by a [deployment](https://modal.com/docs/guide/managing-deployments)
 and call it from other contexts.

There are two ways of invoking deployed Functions. If the invoking client is running Python, then the same [Modal client library](https://pypi.org/project/modal/)
 used to write Modal code can be used. HTTPS is used if the invoking client is not running Python and therefore cannot import the Modal client library.

Invoking with Python 

Some use cases for Python invocation include:

*   An existing Python web server (eg. Django, Flask) wants to invoke Modal Functions.
*   You have split your product or system into multiple Modal Apps that deploy independently and call each other.

### Function lookup and invocation basics 

Let’s say you have a script `my_shared_app.py` and this script defines a Modal App with a Function that computes the square of a number:

    import modal
    
    app = modal.App("my-shared-app")
    
    
    @app.function()
    def square(x: int):
        return x ** 2

You can deploy this App to create a persistent deployment:

    % modal deploy shared_app.py
    ✓ Initialized.
    ✓ Created objects.
    ├── 🔨 Created square.
    ├── 🔨 Mounted /Users/erikbern/modal/shared_app.py.
    ✓ App deployed! 🎉
    
    View Deployment: https://modal.com/apps/erikbern/my-shared-app

Let’s try to run this Function from a different context. For instance, let’s fire up the Python interactive interpreter:

    % python
    Python 3.9.5 (default, May  4 2021, 03:29:30)
    [Clang 12.0.0 (clang-1200.0.32.27)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import modal
    >>> f = modal.Function.from_name("my-shared-app", "square")
    >>> f.remote(42)
    1764
    >>>

This works exactly the same as a regular modal `Function` object. For example, you can `.map()` over Functions invoked this way too:

    >>> f = modal.Function.from_name("my-shared-app", "square")
    >>> f.map([1, 2, 3, 4, 5])
    [1, 4, 9, 16, 25]

#### Authentication 

The Modal Python SDK will read the token from `~/.modal.toml` which typically is created using `modal token new`.

Another method of providing the credentials is to set the environment variables `MODAL_TOKEN_ID` and `MODAL_TOKEN_SECRET`. If you want to call a Modal Function from a context such as a web server, you can expose these environment variables to the process.

#### Lookup of lifecycle functions 

[Lifecycle functions](https://modal.com/docs/guide/lifecycle-functions)
 are defined on classes, which you can look up in a different way. Consider this code:

    import modal
    
    app = modal.App("my-shared-app")
    
    
    @app.cls()
    class MyLifecycleClass:
        @modal.enter()
        def enter(self):
            self.var = "hello world"
    
        @modal.method()
        def foo(self):
            return self.var

Let’s say you deploy this App. You can then call the Function by doing this:

    >>> cls = modal.Cls.from_name("my-shared-app", "MyLifecycleClass")
    >>> obj = cls()  # You can pass any constructor arguments here
    >>> obj.foo.remote()
    'hello world'

### Asynchronous invocation 

In certain contexts, a Modal client will need to trigger Modal Functions without waiting on the result. This is done by spawning functions and receiving a [`FunctionCall`](https://modal.com/docs/sdk/py/latest/modal.FunctionCall)
 as a handle to the triggered execution.

The following is an example of a Flask web server (running outside Modal) which accepts model training jobs to be executed within Modal. Instead of the HTTP POST request waiting on a training job to complete, which would be infeasible, the relevant Modal Function is spawned and the [`FunctionCall`](https://modal.com/docs/sdk/py/latest/modal.FunctionCall)
 object is stored for later polling of execution status.

    from uuid import uuid4
    from flask import Flask, jsonify, request
    
    app = Flask(__name__)
    pending_jobs = {}
    
    ...
    
    @app.route("/jobs", methods = ["POST"])
    def create_job():
        predict_fn = modal.Function.from_name("example", "train_model")
        job_id = str(uuid4())
        function_call = predict_fn.spawn(
            job_id=job_id,
            params=request.json,
        )
        pending_jobs[job_id] = function_call
        return {
            "job_id": job_id,
            "status": "pending",
        }

### Importing a Modal Function between Modal Apps 

You can also import one Function defined in an App from another App:

    import modal
    
    app = modal.App("another-app")
    
    square = modal.Function.from_name("my-shared-app", "square")
    
    
    @app.function()
    def cube(x):
        return x * square.remote(x)
    
    
    @app.local_entrypoint()
    def main():
        assert cube.remote(42) == 74088

### Comparison with HTTPS 

Compared with HTTPS invocation, Python invocation has the following benefits:

*   Uses the Modal client library’s built-in authentication.
    *   Web Functions are public to the entire internet, whereas Function lookup is private to your workspace and authenticated via your Modal token.
*   You can work with shared Modal Functions as if they are normal Python functions, which might be more convenient.

Invoking with HTTPS 

Any application that can make HTTPS requests can interact with deployed Modal Apps via [Web Functions](https://modal.com/docs/guide/webhooks)
. Note that all deployed Web Functions have [a stable HTTPS URL](https://modal.com/docs/guide/webhook-urls)
.

Some use cases for HTTPS invocation include:

*   Calling Modal Functions from a web browser client running JavaScript
*   Calling Modal Functions from backend services in languages we don’t yet have official SDKs for (Java, Ruby, etc.)
*   Calling Modal Functions using UNIX tools (`curl`, `wget`)

However, if the client of your Modal deployment is running Python, JavaScript, or Go, it’s better to use the [Modal Python SDK](https://pypi.org/project/modal/)
 or [Modal SDKs for JavaScript and Go](https://modal.com/docs/guide/sdk-javascript-go)
 to invoke your Modal code.

For more detail on setting up functions for invocation over HTTP see the [Web Functions guide](https://modal.com/docs/guide/webhooks)
.

[Invoking deployed Functions](https://modal.com/docs/guide/trigger-deployed-functions#invoking-deployed-functions)
[Invoking with Python](https://modal.com/docs/guide/trigger-deployed-functions#invoking-with-python)
[Function lookup and invocation basics](https://modal.com/docs/guide/trigger-deployed-functions#function-lookup-and-invocation-basics)
[Authentication](https://modal.com/docs/guide/trigger-deployed-functions#authentication)
[Lookup of lifecycle functions](https://modal.com/docs/guide/trigger-deployed-functions#lookup-of-lifecycle-functions)
[Asynchronous invocation](https://modal.com/docs/guide/trigger-deployed-functions#asynchronous-invocation)
[Importing a Modal Function between Modal Apps](https://modal.com/docs/guide/trigger-deployed-functions#importing-a-modal-function-between-modal-apps)
[Comparison with HTTPS](https://modal.com/docs/guide/trigger-deployed-functions#comparison-with-https)
[Invoking with HTTPS](https://modal.com/docs/guide/trigger-deployed-functions#invoking-with-https)
