[Python] How do I get a list of Single Send campaign names from the Sendgrid API?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I want to use the SendGrid v3 API to retrieve a list of all my Single Send campaigns and their names.

    According to the SendGrid API documentation, I can retrieve statistics for all my Single Sends by sending a GET request to the endpoint /v3/marketing/stats/singlesends. However, the response doesn't include the name of each SingleSend campaign, only message level data like recipient, status, and clicks.

    Example Python code:

    import sendgrid
    import json
    import os

    sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.client.messages.get()
    parsed_json = json.loads(response.body)
    print(json.dumps(parsed_json, indent=4))


    Example response

    {
    "messages": [
    {
    "from_email": "email@example.com",
    "msg_id": "some-message-id-here1",
    "subject": "Message subject here",
    "to_email": "madeline@example.com",
    "status": "processing",
    "opens_count": 0,
    "clicks_count": 0,
    "last_event_time": "2025-05-14T12:00:10Z"
    },
    {
    "from_email": "email@example.com",
    "msg_id": "some-message-id-here2",
    "subject": "Message subject here",
    "to_email": "theo@example.com",
    "status": "delivered",
    "opens_count": 1,
    "clicks_count": 0,
    "last_event_time": "2025-05-14T12:00:08Z"
    }
    ]
    }


    There's also an endpoint for getting Single Send stats by ID, but that only helps if I already have the ID linked to a specific Single Send name.

    Is there an API Endpoint or method to retrieve a list of all my Single Send campaigns and their names?

    Continue reading...

Compartilhe esta Página