---
title: How can I use Python and JavaScript in the same application?
description: Unlock the power of Python and JavaScript in your apps. Learn to integrate Flask and Next.js for dynamic frontends with AI-capable backends. Perfect for developers keen on hybrid programming.
url: /kb/guide/how-to-use-python-and-javascript-in-the-same-application
canonical_url: "https://vercel.com/kb/guide/how-to-use-python-and-javascript-in-the-same-application"
published: 2025-11-03
last_updated: 2026-06-15
authors: Steven Tey
related:
  - /docs/concepts/functions/serverless-functions/runtimes/python
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---
<!-- docsgraph:related -->
## Related pages

> **For AI agents:** Follow these links to understand how this page connects to the rest of the Vercel ecosystem. For the full cross-link map (inbound, outbound, prerequisites, and semantic neighbors), see the .graph.md link below.

- [Flask](https://vercel.com/docs/frameworks/backend/flask?from=related) — Deploy a Flask app on Vercel. Learn how the Python runtime, WSGI, static assets, and Vercel Functions work together.
- [Python](https://vercel.com/docs/functions/runtimes/python?from=related) — Learn how to use the Python runtime to run Python applications on Vercel.
- [FastAPI](https://vercel.com/docs/frameworks/backend/fastapi?from=related) — Deploy a FastAPI app on Vercel. Learn how the Python runtime, ASGI, static assets, and Vercel Functions work together.
- [Django](https://vercel.com/docs/frameworks/full-stack/django?from=related) — Deploy a Django app on Vercel. Learn how the Python runtime, WSGI, ASGI, static assets, and Vercel Functions work togeth
- [Build with a Flask starter template](https://vercel.com/kb/guide/build-with-a-flask-starter-template?from=related) — Deploy a Flask app to Vercel from a starter template. Compare the Flask Hello World starter, AI SDK, alt text generator,
- [How to ship a Flask app on Vercel](https://vercel.com/kb/guide/ship-a-flask-app-on-vercel?from=related) — Deploy a Flask app to Vercel with zero configuration. Learn how to ship from a template, the Vercel CLI, or Git, and con
- [Build with a FastAPI starter template](https://vercel.com/kb/guide/build-with-a-fastapi-starter-template?from=related) — Browse FastAPI starter templates for Vercel and deploy one in a few steps. Compare minimal, AI, agent, and full-stack Fa
- [Using Express.js with Vercel](https://vercel.com/kb/guide/using-express-with-vercel?from=related) — Learn how to use Express.js in a Serverless environment.
- [Does Vercel support Ruby on Rails applications?](https://vercel.com/kb/guide/does-vercel-support-ruby-on-rails-applications?from=related) — Learn how you can use Ruby on Rails with your frontend on Vercel.

Full cross-link map for this page: [/kb/guide/how-to-use-python-and-javascript-in-the-same-application.graph.md](/kb/guide/how-to-use-python-and-javascript-in-the-same-application.graph.md)
<!-- /docsgraph:related -->


Building a hybrid application combining the powers of JavaScript and Python is a great way to leverage the strengths of both languages. This guide will introduce you to a Next.js + Python app, where we use Next.js for the frontend and Flask for the backend API. The benefit of this arrangement is that it allows us to harness the power of Python's extensive AI libraries on the backend while providing a dynamic, responsive frontend with Next.js.

## Architecture Overview

In this hybrid application, the Python/Flask server is integrated into the Next.js app under the `**/api/**` route. This is achieved by using `**next.config.js**` rewrites to route any request to `**/api/:path***` to the Flask API, which is hosted in the `**/api**` folder. On localhost, the rewrite directs to port `**127.0.0.1:5328**` (or any port that you want), where the Flask server runs. In a production setting, the Flask server is hosted as [Python serverless functions on Vercel](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python).

## Option 1: Clone a Template

The easiest way to get this up and running is by cloning our [Next.js Flask Starter template.](https://vercel.com/templates/next.js/nextjs-flask-starter) You can do that by running the following command in your terminal:

```bash
npx create-next-app nextjs-flask --example "https://github.com/vercel/examples/tree/main/python/nextjs-flask"
```

Alternatively, if you prefer [FastAPI](https://fastapi.tiangolo.com/), you can use the [Next.js FastAPI Starter template](https://vercel.com/templates/next.js/nextjs-fastapi-starter) instead:

```bash
npx create-next-app nextjs-fastapi --example "https://github.com/digitros/nextjs-fastapi"
```

## Option 2: Start from Scratch

You can also build this application from scratch by following these steps:

### Step 1: Setting up the Next.js Frontend

Start by bootstrapping your Next.js application with the following command:

```bash
npx create-next-app@latest
```

Create a `**next.config.js**` file in the root directory of your Next.js project. This file will handle routing requests to the Flask API.

Here's a sample `**next.config.js**` file:

```javascript
module.exports = {
  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'http://127.0.0.1:5328/:path*', // Proxy to Backend
      },
    ]
  },
}
```

In the `**next.config.js**` file, the `**rewrites()**` function routes any requests starting with `**/api/**` to the Flask server running on `**http://127.0.0.1:5328**`.

### Step 2: Setting up the Python/Flask Backend

Begin by installing Flask if you haven't done so already:

```bash
pip install flask
```

Now create a new Flask application in the `**/api**` directory. Here's a basic Flask app:

```json
from flask import Flask
app = Flask(__name__)

@app.route('/api/hello', methods=['GET'])
def hello_world():
    return "Hello, World!"

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

This simple Flask app listens for GET requests at the `**/api/hello**` endpoint and responds with "Hello, World!".

### Step 3: Deploying to Vercel

In a production environment, the Flask server can be hosted as Python serverless functions on Vercel. To do this, you need to follow the instructions on the [Vercel documentation](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python) on setting up Python serverless functions.

Once you've done that, any requests to `**/api/:path***` in your Next.js app will be forwarded to your Flask server, whether it's running locally or on Vercel.

## Conclusion

By integrating Flask with Next.js, you can use Python and JavaScript together in the same application. This hybrid approach is perfect for apps that require the power and flexibility of Python's backend capabilities (like AI libraries) with the dynamic frontend capabilities of a Next.js application.