Notification handling
You can optionally configure a notification (sink) URL to receive updates from different functionalities, such as device reachability status or QoD session, deletion and so on. This way, you or your client can receive update notifications from device events and stay in control.
Subscribing to notifications
import network_as_code as nac
from network_as_code.models.device import DeviceIpv4Addr
from network_as_code.models.device_status import EventType, AccessTokenCredential
client = nac.NetworkAsCodeClient(
token="<your-application-key-here>"
)
my_device = client.devices.get(
# Provide either the public and private address
# or the public address and port.
# Devices cannot be identified by their IPv4 address alone.
ipv4_address=DeviceIpv4Addr(
public_address="233.252.0.2",
private_address="192.0.2.25",
public_port=80
),
ipv6_address="2001:db8:1234:5678:9abc:def0:fedc:ba98",
# The phone number does not accept spaces or parentheses
phone_number="+36721601234567"
)
# Here, we will keep track of a devices roaming status
# with a limit of 5 reports
# Simply change the event_type whenever needed
my_subscription = client.device_status.subscribe(
device=my_device,
event_type=[EventType.ROAMING_STATUS],
# Use HTTPS to send notifications
sink="https://example.com/notify",
max_num_of_reports=5,
sink_credential=AccessTokenCredential(
access_token= "some-access-token",
access_token_expires_utc= datetime.now(timezone.utc) + timedelta(days=1),
access_token_type="bearer"
)
)Subscription parameters
| Parameters | Type | Description | Mandatory or Optional |
|---|---|---|---|
event_type | object/string | The status type you want to check. For example EventType.ROAMING_STATUS or org.camaraproject.device-roaming-status-subscriptions.v0.roaming-status. | Mandatory |
device | object | The device object created for the mobile device we want to use. | Mandatory |
sink | string | The recipient's HTTP endpoint, which is a web server configured to receive POST requests. | Mandatory |
sink_credential | string | Contains authorization information for delivery of notifications. | Optional |
credential_type | string | The type of credential. If provided, must be set to "ACCESSTOKEN". | Optional |
access_token | string | Previously acquired access token. | Optional |
access_token_type | string | The type of the access token. If provided, must be set to "bearer". | Optional |
access_token_expires_utc | object/string | When the access token expires. Can be either a date-time object or RFC 3339 formatted date string, for example "2025-12-03T12:27:08.312Z". Must have a time zone. | Optional |
subscription_max_events | integer | Maximum amount of notifications to be sent. Once this amount is reached, the subscription ends. If a notification is sent due to initial_event being set to true, this counts towards the subscription_max_events. | Optional |
Notification handler
The code snippet below will set up an HTTP server with a POST endpoint. Notifications will be sent for when a device is available or not.
NOTE: The notification URL should point to the recipient's HTTP endpoint that will receive the notifications. It needs to be a web server that is configured to receive
POSTrequests that will contain session related updates, such as session creation, deletion, duration, etc. An auth token is also required to identify the sender of the notification. The incomingPOSTrequest will contain the token asAuthorization: Bearer <token> header. Network as Code backend will send it to the informednotification_url. Always specify this parameter when using the notification functionality.
# status_handler.py
# run with: uvicorn status_handler:app
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing_extensions import Annotated
from typing import Optional
app = FastAPI()
class Device(BaseModel):
phoneNumber: Optional[str] | None
networkAccessIdentifier: Optional[str] | None
ipv4Address: Optional[str] | None
ipv6Address: Optional[str] | None
class RoamingEventDetail(BaseModel):
device: Device
subscriptionId: str
roaming: bool | None
countryCode: int | None
countryName: List[str] | None
lastStatusTime: str | None
terminationReason: str
class Event(BaseModel):
eventType: str
eventTime: str
eventDetail: RoamingEventDetail
class Data(BaseModel):
device: Device
subscriptionId: str
terminationReason: str
class Notification(BaseModel):
id: str
source: str
type: str
specversion: str
datacontenttype: str
time: str
eventSubscriptionId: str
event: Event
data: Data
@app.post("/notifications")
def receive_notification(
notification: Notification,
authorization: Annotated[Union[str, None], Header]
):
if authorization == "Bearer my-token":
# We can now react to the notifications
# based on the Notification object
print(notification)Good to remember: The exact implementation of the notification-URL HTTP endpoint, which listens to the incoming
POSTrequests at the/notificationsURL path, can be handled by developers as they see fit.
Where can I use a notification URL and token?
Now that you've learned what a notification URL is and how to create one, you can explore more about the functionalities that use it to notify you of important Network as Code events.
- Check out how to create a QoD session with a notification URL.
- Get device reachability or roaming status notifications.
- Monitor a network slice with notifications.
- Our Network Intelligence product also provides congestion level notifications.
- Easily check if a SIM was swapped with Network as Code SDK and API.
- You can also confirm if a device uses the phone number provided by the user with the Number Verification API.
Last updated December 19, 2025