{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":844,"path":"api-reference/server-api-reference/MIDHermesProducerAPI.md","title":"MIDHermesProducer - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# MIDHermesProducer- Global\n\nThe MIDHermesProducer API provides a method to send data from the MID Server to a Hermes topic.\n\nSending data via the [Hermes Messaging Service](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/servicenow-platform/hermes-messaging-service.md) rather than using the [MID Server ECC Queue](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/servicenow-platform/ecc-queue-mid-server.md) can be a more efficient way to get data to a ServiceNow instance.\n\nThis API requires the ServiceNow MID Hermes API \\(com.glide.mid.hermes\\_api\\) plugin. The calling user must have the kafka\\_admin role. Before calling this API, follow the steps to [Configure the MID Hermes API Extension](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/integrate-applications/MID-hermes-API.md).\n\nUse this API in scripts that run on the MID Server, such as MID Server background scripts or in [Workflow Studio](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/build-workflows/workflow-studio.md) action script steps with the **Required Runtime** set to **MID**.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## MIDHermesProducer - MIDHermesProducer\\(\\)\n\nCreates a MIDHermesProducer for sending messages to Hermes topics.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| ||\n\nThis example instantiates a MIDHermesProducer.\n\n```\nvar producer = new MIDHermesProducer();\n```\n\n## MIDHermesProducer - send\\(Object options\\)\n\nSends a message from the MID Server to the specified Hermes topic.\n\nThe 4000-4050 port range must be open to send messages to a Hermes topic. For more information, see [Producing and consuming messages from a Kafka client](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/servicenow-platform/producing-consuming-hermes.md).\n\n<table id=\"table_n1j_g1c_23c\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\noptions\n\n</td><td>\n\nObject\n\n</td><td>\n\nContains configuration parameters for the message.```\n{\n   applicationId: \"String\",\n   callbackFunction: function(),\n   headers: {Object},\n   key: \"String\",\n   message: \"String\",\n   topic: \"String\"\n}\n```\n\n</td></tr><tr><td>\n\noptions.applicationId\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Application identifier for topic routing. When provided, messages are routed to `snc.{instance}.{applicationId}.{topic}`, otherwise they are routed to `snc.{instance}.{topic}`.\n\n</td></tr><tr><td>\n\noptions.onErrorCallback\n\n</td><td>\n\nFunction\n\n</td><td>\n\nOptional. JavaScript function to execute if message delivery fails. The function accepts two parameters: String `errorMessage` and String `errorType` \\(the fully qualified Java exception class name\\).```\nfunction(errorMessage, errorType) { \n   //implement error handling here\n};\n```\n\n</td></tr><tr><td>\n\noptions.headers\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. Key-value pairs for message headers. Any headers supported by Apache Kafka are valid.\n\n</td></tr><tr><td>\n\noptions.key\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Message key for partitioning in Apache Kafka. Messages with the same key are sent to the same partition, ensuring ordering.\n\n</td></tr><tr><td>\n\noptions.message\n\n</td><td>\n\nString\n\n</td><td>\n\nContent of the message to send to the topic. Any value formatted as a string is valid.\n\n</td></tr><tr><td>\n\noptions.topic\n\n</td><td>\n\nString\n\n</td><td>\n\nName of the Hermes topic to send the message to.**Note:** Don't include the instance prefix; it is added automatically.\n\n</td></tr></tbody>\n</table><table id=\"table_o1j_g1c_23c\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nNone\n\n</td><td>\n\nThis method does not return a value. Message delivery is asynchronous. Success: Messages are delivered asynchronously to Hermes Kafka brokers. Check MID Server logs for delivery confirmation.\n\nFailure: If message delivery fails, the optional callback function \\(if provided\\) is invoked with error details. Common errors include:\n\n-   `org.apache.kafka.common.errors.TimeoutException`: Kafka broker unreachable or network issues. Check firewall rules for Hermes bootstrap address and ports.\n-   `com.service_now.mid.hermes_api.exception.MIDHermesAPIExtensionNotStartedException`: Extension not started. Verify MID Hermes API extension context is active.\n-   `java.lang.IllegalArgumentException`: Invalid parameters. Ensure topic and message are provided.\n\nAdditional steps to resolve errors:\n\n-   Verify MID Hermes API extension is running in MID Server UI.\n-   Check network connectivity to Hermes.\n-   Review MID Server logs with property **mid.hermes.debug=true** for detailed diagnostics.\n-   Confirm IPKI certificate is valid in MID Server keystore.\n\n</td></tr></tbody>\n</table>This example sends a message to a Hermes topic.\n\n```\n// Create producer instance \nvar producer = new MIDHermesProducer(); \n\n// Define error callback function \nvar errorCallback = function(errorMessage, errorType) {\n    gs.error(\"Message delivery failed: \" + errorType + \" - \" + errorMessage);\n}; \n\n// Send message with all optional parameters \nproducer.send({ \n    topic: \"order-events\", \n    applicationId: \"sn_streamconnect\", \n    message: JSON.stringify({ \n        orderId: \"ORD-12345\", \n        status: \"completed\", \n        timestamp: new Date().getTime() \n    }), \n    key: \"customer-456\", \n    headers: { \n        \"content-type\": \"application/json\", \n        \"source\": \"mid-server\", \n        \"version\": \"1.0\" \n    }, \n    onErrorCallback: errorCallback \n});\n```\n\nThe message is sent asynchronously. Check MID Server logs for delivery confirmation.\n\n"}}