Skip to main content
Hooks allow you to customize Auth0’s behavior by executing custom code at specific extension points in the authentication pipeline.

List Hooks

Retrieve all hooks, optionally filtered by trigger.
func (c *Client) List(
    ctx context.Context,
    request *management.ListHooksRequestParameters,
    opts ...option.RequestOption,
) (*core.Page[*int, *management.Hook, *management.ListHooksOffsetPaginatedResponseContent], error)
request
*management.ListHooksRequestParameters
Optional query parameters for listing hooks
*core.Page[*int, *management.Hook, *management.ListHooksOffsetPaginatedResponseContent]
Paginated list of hooks

Example

page, err := managementClient.Hooks.List(context.Background(), &management.ListHooksRequestParameters{
    TriggerID: management.HookTriggerIDEnumCredentialsExchange.Ptr(),
    PerPage:   auth0.Int(10),
})
if err != nil {
    // Handle error
}

for _, hook := range page.Results {
    fmt.Printf("Hook: %s (enabled: %v)\n", hook.GetName(), hook.GetEnabled())
}

Create Hook

Create a new hook.
func (c *Client) Create(
    ctx context.Context,
    request *management.CreateHookRequestContent,
    opts ...option.RequestOption,
) (*management.CreateHookResponseContent, error)
request
*management.CreateHookRequestContent
required
The hook to create
*management.CreateHookResponseContent
The created hook with server-generated fields

Example

hook, err := managementClient.Hooks.Create(context.Background(), &management.CreateHookRequestContent{
    TriggerID: auth0.String("credentials-exchange"),
    Name:      auth0.String("Add Custom Claims"),
    Script:    auth0.String(`
module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token['https://example.com/claim'] = 'custom_value';
  cb(null, access_token);
};
    `),
    Enabled: auth0.Bool(true),
})
if err != nil {
    // Handle error
}
fmt.Printf("Created hook: %s\n", hook.GetID())

Get Hook

Retrieve a specific hook by ID.
func (c *Client) Get(
    ctx context.Context,
    id string,
    request *management.GetHookRequestParameters,
    opts ...option.RequestOption,
) (*management.GetHookResponseContent, error)
id
string
required
ID of the hook to retrieve
request
*management.GetHookRequestParameters
Optional parameters
*management.GetHookResponseContent
The requested hook

Example

hook, err := managementClient.Hooks.Get(context.Background(), "hook_abc123", &management.GetHookRequestParameters{})
if err != nil {
    // Handle error
}
fmt.Printf("Hook: %s\n", hook.GetName())

Update Hook

Update an existing hook.
func (c *Client) Update(
    ctx context.Context,
    id string,
    request *management.UpdateHookRequestContent,
    opts ...option.RequestOption,
) (*management.UpdateHookResponseContent, error)
id
string
required
ID of the hook to update
request
*management.UpdateHookRequestContent
required
Updated hook data
*management.UpdateHookResponseContent
The updated hook

Example

hook, err := managementClient.Hooks.Update(context.Background(), "hook_abc123", &management.UpdateHookRequestContent{
    Enabled: auth0.Bool(false),
})
if err != nil {
    // Handle error
}

Delete Hook

Delete a hook.
func (c *Client) Delete(
    ctx context.Context,
    id string,
    opts ...option.RequestOption,
) error
id
string
required
ID of the hook to delete

Example

err := managementClient.Hooks.Delete(context.Background(), "hook_abc123")
if err != nil {
    // Handle error
}

Hook Triggers

Hooks support the following trigger points:
  • credentials-exchange: Customize access token claims during client credentials exchange
  • pre-user-registration: Execute code before a user registration
  • post-user-registration: Execute code after a user registration
  • post-change-password: Execute code after a password change
  • send-phone-message: Customize phone verification messages
const (
    HookTriggerIDEnumCredentialsExchange  = "credentials-exchange"
    HookTriggerIDEnumPreUserRegistration  = "pre-user-registration"
    HookTriggerIDEnumPostUserRegistration = "post-user-registration"
    HookTriggerIDEnumPostChangePassword   = "post-change-password"
    HookTriggerIDEnumSendPhoneMessage     = "send-phone-message"
)

Hook Secrets

Manage sensitive configuration for hooks using the Secrets sub-resource:
// Add a secret
err := managementClient.Hooks.Secrets.Create(context.Background(), "hook_abc123", &management.CreateSecretRequest{
    Key:   auth0.String("API_KEY"),
    Value: auth0.String("secret-value"),
})

// List secrets (values are not returned)
secrets, err := managementClient.Hooks.Secrets.List(context.Background(), "hook_abc123")

// Delete a secret
err = managementClient.Hooks.Secrets.Delete(context.Background(), "hook_abc123", "API_KEY")

Complete Example

package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/auth0/go-auth0/v2"
    "github.com/auth0/go-auth0/v2/management"
)

func main() {
    managementClient, err := management.New(
        "yourtenant.auth0.com",
        management.WithClientCredentials(clientID, clientSecret),
    )
    if err != nil {
        log.Fatal(err)
    }
    
    ctx := context.Background()
    
    // Create a hook with dependencies
    hook, err := managementClient.Hooks.Create(ctx, &management.CreateHookRequestContent{
        TriggerID: auth0.String("credentials-exchange"),
        Name:      auth0.String("Custom Claims Hook"),
        Script: auth0.String(`
module.exports = function(client, scope, audience, context, cb) {
  const axios = require('axios');
  // Custom logic here
  cb(null, {});
};
        `),
        Dependencies: &management.HookDependencies{
            "axios": "^0.21.1",
        },
        Enabled: auth0.Bool(true),
    })
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("Created hook: %s\n", hook.GetID())
}

Build docs developers (and LLMs) love