Yes, there is a way using the REST API to obtain a function access code. The following are the steps:
Let assume a name of the function is EventGridTrigger2 and the run.csx:
#r "Newtonsoft.Json"
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static void Run(JObject eventGridEvent, TraceWriter log)
{
log.Info(eventGridEvent.ToString(Formatting.Indented));
}
and the function.json file:
{
"bindings": [
{
"type": "eventGridTrigger",
"name": "eventGridEvent",
"direction": "in"
}
],
"disabled": false
}
As you can see the above bindings is untyped, which will work for any output schema such as InputEventSchema, EventGridSchema (default schema) and CloudEventV01Schema (after fixing some bug).
The destination property of the created Subscription looks like the following:
"destination": {
"properties": {
"endpointUrl": null,
"endpointBaseUrl": "https://myFunctionApp.azurewebsites.net/admin/extensions/EventGridExtensionConfig"
},
"endpointType": "WebHook"
},
Note, that the full subscriberUrl for Azure EventGrid trigger has the following format, where the query string contains parameters for routing request to the properly function:
https://{FunctionApp}.azurewebsites.net/admin/extensions/EventGridExtensionConfig?functionName={FunctionName}&code={masterKey}
For creating a subscriber we have to use its full subscriberUrl included a query string. In this moment, the only unknown value is the masterKey.
To obtain a Function App (Host) masterkey we have to use a management REST API call:
https://management.azure.com/subscriptions/{mySubscriptionId}/resourceGroups/{myResGroup}/providers/Microsoft.Web/sites/{myFunctionApp}/functions/admin/masterkey?api-version=2016-08-01
the response has the following format:
{
"masterKey": "*************************************************"
}
Note, that the authentication Bearer token is required for this call.
Once we have a masterKey for the FunctionApp (host), we can use it for any function within this host.