GitHub user dosubot[bot] added a comment to the discussion: How can i create a 
new endpoint?

<!-- Answer -->
To create a new API endpoint in Superset, define a new API class (usually 
subclassing `BaseSupersetModelRestApi` or a similar base), add methods 
decorated with `@expose` for your routes, and register your class with Flask 
AppBuilder using `appbuilder.add_api`. For example:

```python
from flask_appbuilder.api import BaseApi, expose
from flask import Response

class MyCustomApi(BaseApi):
    resource_name = "my_custom"
    allow_browser_login = True

    @expose("/hello", methods=["GET"])
    def hello(self):
        return self.response(200, message="Hello from custom endpoint!")
```

Then, register your API in the initialization code (e.g., in 
`superset/initialization/__init__.py`):

```python
appbuilder.add_api(MyCustomApi)
```

You can see how Superset registers built-in APIs this way in its initialization 
logic 
([example](https://github.com/apache/superset/blob/master/superset/initialization/__init__.py)).

You do not need to rebuild the Docker image if you mount your code as a volume 
or use a plugin architecture in development mode—just update the code and 
restart the Superset server if needed. This allows for hot-reloading or quick 
iteration without image rebuilds 
([details](https://github.com/apache/superset/blob/master/superset/dashboards/api.py)).
 

You cannot add new endpoints via `superset_config.py` alone; you must add code 
to the Superset codebase or a plugin. 

Let me know if you want a more detailed example or have questions about 
registering endpoints in a plugin!


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/5fd4c409-fd84-4ac6-a9a6-a0db7ff40fd1?feedback_type=other)</sup>

[![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=To%20create%20a%20new%20API%20endpoint%20in%20Superset%2C%20define%20a%20new%20API%20class%20%28usually%20subclassing%20%60BaseSupersetModelRestApi%60%20or%20a%20similar%20base%29%2C%20add%20methods%20decorated%20with%20%60%40expose%60%20for%20your%20routes%2C%20and%20register%20your%20class%20with%20Flask%20AppBuilder%20using%20%60appbuilder.add_api%60.%20For%20example%3A%0A%0A%60%60%60python%0Afrom%20flask_appbuilder.api%20import%20BaseApi%2C%20expose%0Afrom%20flask%20import%20Response%0A%0Aclass%20MyCustomApi%28BaseApi%29%3A%0A%20%20%20%20resource_name%20%3D%20%22my_custom%22%0A%20%20%20%20allow_browser_login%20%3D%20True%0A%0A%20%20%20%20%40expose%28%22/hello%22%2C%20methods%3D%5B%22GET%22%5D%29%0A%20%20%20%20def%20hello%28self%29%3A%0A%20%20%20%2
 
0%20%20%20%20return%20self.response%28200%2C%20message%3D%22Hello%20from%20custom%20endpoint%21%22%29%0A%60%60%60%0A%0AThen%2C%20register%20your%20API%20in%20the%20initialization%20code%20%28e.g.%2C%20in%20%60superset/initialization/__init__.py%60%29%3A%0A%0A%60%60%60python%0Aappbuilder.add_api%28MyCustomApi%29%0A%60%60%60%0A%0AYou%20can%20see%20how%20Superset%20registers%20built-in%20APIs%20this%20way%20in%20its%20initialization%20logic%20%28%5Bexample%5D%28https%3A//github.com/apache/superset/blob/master/superset/initialization/__init__.py%29%29.%0A%0AYou%20do%20not%20need%20to%20rebuild%20the%20Docker%20image%20if%20you%20mount%20your%20code%20as%20a%20volume%20or%20use%20a%20plugin%20architecture%20in%20development%20mode%E2%80%94just%20update%20the%20code%20and%20restart%20the%20Superset%20server%20if%20needed.%20This%20allows%20for%20hot-reloading%20or%20quick%20iteration%20without%20image%20rebuilds%20%28%5Bdetails%5D%28https%3A//github.com/apache/superset/blob/master/superse
 
t/dashboards/api.py%29%29.%20%0A%0AYou%20cannot%20add%20new%20endpoints%20via%20%60superset_config.py%60%20alone%3B%20you%20must%20add%20code%20to%20the%20Superset%20codebase%20or%20a%20plugin.%20%0A%0ALet%20me%20know%20if%20you%20want%20a%20more%20detailed%20example%20or%20have%20questions%20about%20registering%20endpoints%20in%20a%20plugin%21)&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/37144)

GitHub link: 
https://github.com/apache/superset/discussions/37144#discussioncomment-15501488

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to