Real-time API observability: Using Moesif and Heroku Together

Heroku provides a robust platform to deploy and scale your applications. However, inherent complexities within your app’s performance and user interaction remain obscured. Your app may be encountering errors and performance bottlenecks. Moreover, a lack of API analytics and API observability impedes your ability to truly grow your product.
With Moesif, you can easily gain that visibility into your APIs on Heroku. It integrates with your Heroku app as an add-on in two simple steps. In this article, we walk you through deploying a Django application on Heroku and then integrating Moesif with it. Then we’ll see some of the features Moesif offers for deep API observability and analytics.
Table of Contents
- Why Moesif and Heroku work better together
- Prerequisites
- Set up your Django application
- Create and deploy the Heroku app
- Set up Moesif with Heroku
- Access Moesif from Heroku
- Usage examples
- Wrapping up
- Next Steps
Why Moesif and Heroku work better together
- Install and set up Moesif directly from Heroku Elements Marketplace.
- Access, manage, and use Moesif through Heroku CLI or Heroku Dashboard.
- Pay for Moesif through Heroku without having to manage a separate subscription.
- Automatically monitor API traffic and gain deep insights into your app’s adoption, customer behavior, and profitability.
You don’t have to manually instrument your app or define collection procedures for analytics data. Moesif automatically captures and collects API requests and responses, including headers, HTTP bodies, query parameters, and associated customer data.
Prerequisites
Before proceeding, make sure you have active accounts in Heroku and Moesif. Then install the Heroku CLI so you can deploy, manage, and run your application from the command line. After you’ve installed the Heroku CLI, log into your Heroku account:
heroku login
Set up your Django application
-
Clone our Django example application from GitHub and enter the root directory.
git clone https://github.com/Moesif/moesifdjangoexample cd moesifdjangoexample
-
The
requirements-django4.txt
file in the example app contains more recent versions of the app’s dependencies. Therefore, we recommend you use that for your Heroku app’srequirements.txt
file.
Then, addgunicorn
as a dependency in therequirements.txt
file. Gunicorn is a Python WSGI HTTP server that Heroku uses to serve your application. In the next step, we define the exactgunicorn
command that starts the app. After adding Gunicorn, yourrequirements.txt
file should look like this:Django==4.2.0 moesifdjango==2.3.12 djangorestframework==3.15.0 gunicorn
-
Add a Procfile in the root directory of the application that specifies the following command:
web: gunicorn mysite.wsgi
-
In
mysite/settings.py
, define the Django secret key as an environment variable to fetch from a Heroku config var:SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")
In the next section, you’ll create a Heroku app and then add the
DJANGO_SECRET_KEY
config var to the app. -
In
mysite/settings.py
, configureALLOWED_HOSTS
:IS_HEROKU_APP = "DYNO" in os.environ and "CI" not in os.environ if IS_HEROKU_APP: ALLOWED_HOSTS = ["*"] SECURE_SSL_REDIRECT = True else: ALLOWED_HOSTS = ["127.0.0.1:8006", "127.0.0.1:8000", "127.0.0.1", "localhost"]
-
We recommend using Python version 3.12.3 with the example application. Therefore, specify the version in the
.python-version
file in the root directory of the app:3.12.3
-
The example Django app doesn’t contain any static files. So disable running Django’s
collectstatic
command when Heroku builds the app:heroku config:set DISABLE_COLLECTSTATIC=1
Create and deploy the Heroku app
Make sure you’re in the root directory of the application. Then follow these steps:
-
Create your Heroku app with the following command:
heroku create
-
Deploy your application code to Heroku:
git push heroku master
After the build and deployment finishes successfully, the Heroku CLI shows you the URL where it hosts your app at the end of the logs. For example:
... -----> Restoring cache -----> Using cached install of Python 3.12.3 -----> Installing pip 24.3.1, setuptools 70.3.0 and wheel 0.45.1 -----> Installing SQLite3 -----> Installing dependencies using 'pip install -r requirements.txt' -----> Skipping Django collectstatic since the env var DISABLE_COLLECTSTATIC is set. -----> Discovering process types Procfile declares types -> web -----> Compressing... Done: 28.8M -----> Launching... Released v12 https://YOUR_APP_NAME-12345678xy12.herokuapp.com/ deployed to Heroku
Set up Moesif with Heroku
-
Install Moesif add-on:
heroku addons:create moesif
During the installation process, Heroku informs you about creating a
MOESIF_APPLICATION_ID
config var. You also can get this application ID in the next step. -
Install Moesif server integration for your Heroku app:
a. Go to your Heroku dashboard and select your app.
b. In the Installed add-ons section, select the Moesif add-on.
Selecting the Moesif add-on in Heroku Dashboard This takes you through the Moesif onboarding steps.
Moesif Web Portal onboarding screen During these steps, Moesif shows you your Application ID that Heroku puts in its
MOESIF_APPLICATION_ID
config var when installing the Moesif add-on.
After you finish the onboarding steps, Moesif integration with your Heroku app should complete, giving green checkmarks for all four steps like in the preceding image.
To check if everything is working correctly, send an HTTP request to your application endpoint. For example, sending a POST
request to https://YOUR_APP_NAME-12345678xy12.herokuapp.com/api/users/66640/
returns the following response:
{
"user_id": "66640",
"update_users": "success"
}
Moesif should also capture the event and log its details. In your Moesif Web Portal, you can see the event’s details in the Live Event Log.
For the available API endpoints in the Django example application, see the urls.py
files.
Access Moesif from Heroku
You can always access your Moesif Web Portal from Heroku CLI with the following command:
heroku addons:moesif open
Alternatively, go to your Heroku Dashboard, select your app, and then select Moesif add-on from the Installed add-ons section.
Both of these methods allow you to log into Moesif and access your Heroku app’s analytics.
Usage examples
WIth Moesif integrated with your Heroku app, you have a dedicated platform that unifies observability, analytics, and monetization in one place. Here are some usage examples to demonstrate how Moesif can improve your Heroku application’s monitoring and insights:
Debug API failures
Your application may rely on external APIs or services. If you spot API calls failing intermittently, the root cause can likely lie elsewhere.
Since Moesif captures HTTP requests and responses with greater context, including payloads, it makes debugging such issues much easier. It improves correlating with Heroku logs and metrics for API failures, errors, and contextual information like HTTP headers. Furthermore, if you’ve instrumented your app with OpenTelemetry, you can use Moesif’s OpenTelemetry support to get trace data straight into your Moesif analytics dashboards.
Deep API analytics for API endpoints
Consider the example Django application. It essentially serves a REST API using the Django REST framework. With Moesif integration, you can track the API’s performance, error rates, and user behavior.
You can leverage a rich set of filters, segmentation, and analysis types to analyze traffic for each endpoint, identify bottlenecks, and understand usage trends. This provides insights into API clients making the most requests and how they interact with different endpoints.
For example, you can use Live Event Log to dig into recent 4xx
and 5xx
errors:

You can also get a breakdown of 4xx
client errors by endpoints using a Segmentation chart:

Detect and block API abuse in real time
You may notice your API experiencing excessive traffic from certain IP addresses or API tokens. This can potentially lead to rate-limiting or security risks. In situations like these, here’s how Moesif can help:
- Detect anomalous traffic patterns, like a single user making thousands of requests in a short time, and block them from further access with Quotas and Governance:
Detecting and blocking API access using Moesif’s governance rules - Establish automated and real-time alerts that you can customize according to your needs..
- Perform heatmap analysis and get geo-location insights to identify traffic from suspicious regions.
Tracking Business Value of APIs
If you have a SaaS API that customers use to integrate your services into their workflows, you can analyze consumption of your APIs using Moesif:
- Understand how customers interact with your API over time.
- Segment users based on different criteria, for example, active versus inactive users, demographics, and so on.
- Track adoption of APIs such as through conversion funnels.
Understanding your customers allows you to identify issues like onboarding friction. You can make decisive decisions to work on improving customer experience and send targeted onboarding emails.
Improve API monetization
If you’re monetizing your API, through, for example, a usage-based pricing model, you can leverage Moesif’s robust usage metering and tracking features.
Moesif integrates with popular billing providers like Stripe. You can also integrate a custom billing solution with Moesif where it accurately tracks, meters, and reports usage. Through Billing Report Metrics, you can get detailed usage reports for metered billing.
For example, you may face a customer dispute that claims an incorrect charge amount. With Moesif, you can use Billing Report Metrics and a per-user API request log during the billing period, to investigate and resolve the issue.
Wrapping up
This article uses a Python Django app for demonstration, but both Heroku and Moesif support a variety of application frameworks and tools. Heroku’s scalable, fully-managed services combined with Moesif’s API intelligence can help you scale your app while maintaining deep observability and security.
To try out Moesif for yourself and see how it can help you build better products, sign up today for a free trial, no credit cards required.
Next Steps
- Integrate Moesif with your platform of choice.
- Learn about dashboards and workspaces.
- Achieve API traceability with Moesif’s OpenTelemetry integration.
- Use Moesif to understand revenue and cost of API usage.