Skip to main content

Check out Port for yourselfย 

Toggle LaunchDarkly Feature Flag

Overviewโ€‹

This guide will help you implement a self-service action in Port that allows you to toggle LaunchDarkly feature flags directly from Port. This functionality streamlines feature management by enabling users to control feature flags without leaving Port.

You can implement this action in two ways:

  1. GitHub workflow: A more flexible approach that allows for complex workflows and custom logic, suitable for teams that want to maintain their automation in Git.
  2. Synced webhooks: A simpler approach that directly interacts with LaunchDarkly's API through Port, ideal for quick implementation and minimal setup.

Prerequisitesโ€‹

  • Complete the onboarding process.

  • Access to your LaunchDarkly organization with permissions to manage feature flags.

  • A LaunchDarkly API token with permission to toggle feature flags. Learn more

  • Optional - Install Port's LaunchDarkly integration learn more

    LaunchDarkly Integration

    This step is not required for this example, but it will create all the blueprint boilerplate for you, and also ingest and update the catalog in real time with your LaunchDarkly Feature Flags.

Set up data modelโ€‹

If you haven't installed the LaunchDarkly integration, you'll need to create a blueprint for LaunchDarkly feature flags. However, we highly recommend you install the LaunchDarkly integration to have these automatically set up for you.

Create the LaunchDarkly feature flag blueprint

  1. Go to your Builder page.

  2. Click on + Blueprint.

  3. Click on the {...} button in the top right corner, and choose "Edit JSON".

  4. Add this JSON schema:

    LaunchDarkly Feature Flag Blueprint (Click to expand)
    {
    "identifier": "launchDarklyFeatureFlag",
    "description": "This blueprint represents a feature flag in LaunchDarkly.",
    "title": "LaunchDarkly Feature Flag",
    "icon": "Launchdarkly",
    "schema": {
    "properties": {
    "kind": {
    "type": "string",
    "title": "Flag Kind",
    "description": "The type of the feature flag (e.g., boolean)."
    },
    "description": {
    "type": "string",
    "title": "Description",
    "description": "A description of what the flag controls."
    },
    "creationDate": {
    "type": "string",
    "format": "date-time",
    "title": "Creation Date",
    "description": "The date and time when the flag was created."
    },
    "clientSideAvailability": {
    "type": "object",
    "title": "Client-Side Availability",
    "description": "Availability of the flag for client-side applications."
    },
    "temporary": {
    "type": "boolean",
    "title": "Temporary Flag",
    "description": "Indicates if the flag is temporary."
    },
    "tags": {
    "type": "array",
    "title": "Tags",
    "description": "Tags associated with the feature flag."
    },
    "maintainer": {
    "type": "string",
    "title": "Maintainer",
    "description": "Email address of the maintainer of the flag."
    },
    "customProperties": {
    "type": "object",
    "title": "Custom Properties",
    "description": "Custom properties associated with the flag."
    },
    "archived": {
    "type": "boolean",
    "title": "Archived",
    "description": "Indicates if the flag is archived."
    },
    "deprecated": {
    "type": "boolean",
    "title": "Deprecated",
    "description": "Indicates if the flag is deprecated."
    },
    "variations": {
    "type": "array",
    "title": "Variations",
    "description": "An array of possible variations for the flag"
    }
    },
    "required": []
    },
    "mirrorProperties": {},
    "calculationProperties": {},
    "aggregationProperties": {},
    "relations": {
    "environments": {
    "title": "Environments",
    "target": "launchDarklyEnvironment",
    "required": false,
    "many": true
    }
    }
    }
  5. Click "Save" to create the blueprint.

Implementationโ€‹

You can toggle LaunchDarkly feature flags by leveraging Port's synced webhooks and secrets to directly interact with the LaunchDarkly's API. This method simplifies the setup by handling everything within Port.

Add Port secrets

Existing secrets

If you have already installed Port's LaunchDarkly integration, these secrets should already exist in your portal.
To view your existing secrets:

  1. Click on the ... button in the top right corner of your Port application.
  2. Choose Credentials, then click on the Secrets tab.

To add these secrets to your portal:

  1. Click on the ... button in the top right corner of your Port application.

  2. Click on Credentials.

  3. Click on the Secrets tab.

  4. Click on + Secret and add the following secrets:

    • LAUNCHDARKLY_ACCESS_TOKEN: Your LaunchDarkly API token

Set up self-service action

Follow these steps to create the self-service action:

  1. Head to the self-service page.

  2. Click on the + New Action button.

  3. Click on the {...} Edit JSON button.

  4. Copy and paste the following JSON configuration into the editor.

    Toggle LaunchDarkly Feature Flag (Webhook) (Click to expand)
    {
    "identifier": "toggle_feature_flag_webhook",
    "title": "Toggle Feature Flag (Webhook)",
    "icon": "Launchdarkly",
    "description": "Toggle a LaunchDarkly feature flag using a webhook",
    "trigger": {
    "type": "self-service",
    "operation": "DAY-2",
    "userInputs": {
    "properties": {
    "project_key": {
    "description": "LaunchDarkly Project Key",
    "title": "Project Key",
    "icon": "Launchdarkly",
    "type": "string"
    },
    "flag_key": {
    "description": "LaunchDarkly Flag Key (without project suffix)",
    "title": "Flag Key",
    "icon": "Launchdarkly",
    "type": "string"
    },
    "environment_key": {
    "description": "LaunchDarkly Environment Key where the flag exists",
    "title": "Environment Key",
    "icon": "Launchdarkly",
    "type": "string"
    },
    "flag_state": {
    "title": "Flag State",
    "description": "Desired state of the feature flag (true for enabled, false for disabled)",
    "icon": "Launchdarkly",
    "type": "boolean",
    "default": true
    }
    },
    "required": [
    "project_key",
    "flag_key",
    "environment_key"
    ],
    "order": [
    "project_key",
    "flag_key",
    "environment_key",
    "flag_state"
    ]
    },
    "blueprintIdentifier": "launchDarklyFeatureFlag"
    },
    "invocationMethod": {
    "type": "WEBHOOK",
    "url": "https://app.launchdarkly.com/api/v2/flags/{{.inputs.project_key}}/{{.inputs.flag_key}}",
    "agent": false,
    "synchronized": true,
    "method": "PATCH",
    "headers": {
    "Authorization": "{{.secrets.LAUNCHDARKLY_ACCESS_TOKEN}}",
    "Content-Type": "application/json"
    },
    "body": [
    {
    "op": "replace",
    "path": "/environments/{{.inputs.environment_key}}/on",
    "value": "{{.inputs.flag_state}}"
    }
    ]
    },
    "requiredApproval": false
    }
  5. Click Save.

Now you should see the Toggle Feature Flag (Webhook) action in the self-service page. ๐ŸŽ‰

Create an automation to update entity in port

To keep your catalog updated with the latest feature flag state, you can create an automation that will update the LaunchDarkly feature flag entity in Port immediately after the webhook action completes successfully.

Follow these steps to add the automation:

  1. Head to the automation page.

  2. Click on the + Automation button.

  3. Copy and paste the following JSON configuration into the editor.

    Update LaunchDarkly feature flag in Port automation (Click to expand)
    {
    "identifier": "launchDarklyFeatureFlag_sync_after_toggle",
    "title": "Sync LaunchDarkly Feature Flag After Toggle",
    "description": "Update LaunchDarkly feature flag data in Port after toggling",
    "trigger": {
    "type": "automation",
    "event": {
    "type": "RUN_UPDATED",
    "actionIdentifier": "toggle_feature_flag_webhook"
    },
    "condition": {
    "type": "JQ",
    "expressions": [
    ".diff.after.status == \"SUCCESS\""
    ],
    "combinator": "and"
    }
    },
    "invocationMethod": {
    "type": "UPSERT_ENTITY",
    "blueprintIdentifier": "launchDarklyFeatureFlag",
    "mapping": {
    "identifier": "{{.event.diff.after.entity.identifier}}",
    "title": "{{.event.diff.after.entity.title}}",
    "properties": {
    "kind": "{{.event.diff.after.entity.properties.kind}}",
    "description": "{{.event.diff.after.entity.properties.description}}",
    "temporary": "{{.event.diff.after.entity.properties.temporary}}",
    "tags": "{{.event.diff.after.entity.properties.tags}}",
    "archived": "{{.event.diff.after.entity.properties.archived}}",
    "deprecated": "{{.event.diff.after.entity.properties.deprecated}}"
    }
    }
    },
    "publish": true
    }
  4. Click Save.

Now when you execute the webhook action, the feature flag data in Port will be automatically updated with the latest information from LaunchDarkly.

Let's test it!โ€‹

  1. Head to the self-service page of your portal

  2. Choose either the GitHub workflow or webhook implementation:

    • For GitHub workflow: Click on Toggle LaunchDarkly Feature Flag
    • For webhook: Click on Toggle Feature Flag (Webhook)
  3. Select the LaunchDarkly feature flag you want to toggle

  4. Enter the required information:

    • Project Key: The LaunchDarkly project key where the flag is located
    • Flag Key: The LaunchDarkly flag key (without project suffix)
    • Environment Key: The environment where you want to toggle the flag
    • Flag State: Set to true to enable the flag or false to disable it
  5. Click on Execute

  6. Done! Wait for the feature flag's status to be changed in LaunchDarkly

More relevant guides and examplesโ€‹