Define custom blocks
Stay organized with collections
Save and categorize content based on your preferences.
Blocks are what you use to program. They represent expressions and statements in
text-based programming languages.
For more information about blocks and their parts, see the
visual glossary.
Block definition
A block definition specifies the puzzle piece connections and fields on your
block. Most of the look and style of your blocks is specified in other ways. The
string (usually code) your block gets converted to is defined as a block-code
generator.
The easiest way to define simple blocks is using JSON.
This code snippet defines a "move forward" block with next and previous
connections, and one field for the distance.
// Create the definition.
const definitions = Blockly.common.createBlockDefinitionsFromJsonArray([
{
// The type is like the "class name" for your block. It is used to construct
// new instances. E.g. in the toolbox.
type: 'my_custom_block',
// The message defines the basic text of your block, and where inputs or
// fields will be inserted.
message0: 'move forward %1',
args0: [
// Each arg is associated with a %# in the message.
// This one gets substituted for %1.
{
// The type specifies the kind of input or field to be inserted.
type: 'field_number',
// The name allows you to reference the field and get its value.
name: 'FIELD_NAME',
}
],
// Adds an untyped previous connection to the top of the block.
previousStatement: null,
// Adds an untyped next connection to the bottom of the block.
nextStatement: null,
}
]);
// Register the definition.
Blockly.common.defineBlocks(definitions);
For more information about how to define your blocks and add them to your
toolbox, see Custom blocks overview.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["\u003cp\u003eBlocks are the fundamental units used for programming in Blockly, representing expressions and statements like in text-based languages.\u003c/p\u003e\n"],["\u003cp\u003eA block definition determines its connections and fields, essentially shaping the block's structure and interactivity.\u003c/p\u003e\n"],["\u003cp\u003eBlockly utilizes JSON to simplify the process of defining blocks, allowing customization of text, inputs, and connections.\u003c/p\u003e\n"],["\u003cp\u003eUsers can define their own custom blocks by specifying parameters such as type, message, arguments, and connections using JSON.\u003c/p\u003e\n"],["\u003cp\u003eDefined blocks need to be registered and then included in the toolbox for users to access and utilize them within Blockly's workspace.\u003c/p\u003e\n"]]],["Blocks represent code expressions and statements. Block definitions specify connections and fields, while code generation defines the resulting code. The simplest method for defining blocks is JSON. A \"move forward\" block example shows the use of `type`, `message0`, `args0`, `previousStatement`, and `nextStatement` to define its structure. The block is registered using `Blockly.defineBlocks()`. Additional information is available on defining blocks and using the toolbox.\n"],null,["# Define custom blocks\n\nBlocks are what you use to program. They represent expressions and statements in\ntext-based programming languages.\n\nFor more information about blocks and their parts, see the\n[visual glossary](/blockly/guides/get-started/workspace-anatomy#blocks).\n\nBlock definition\n----------------\n\nA block definition specifies the puzzle piece connections and fields on your\nblock. Most of the look and style of your blocks is specified in other ways. The\nstring (usually code) your block gets converted to is defined as a [block-code\ngenerator](/blockly/guides/get-started/code-generation).\n\nThe easiest way to define simple blocks is using JSON.\n\nThis code snippet defines a \"move forward\" block with next and previous\nconnections, and one field for the distance. \n\n // Create the definition.\n const definitions = Blockly.common.createBlockDefinitionsFromJsonArray([\n {\n // The type is like the \"class name\" for your block. It is used to construct\n // new instances. E.g. in the toolbox.\n type: 'my_custom_block',\n // The message defines the basic text of your block, and where inputs or\n // fields will be inserted.\n message0: 'move forward %1',\n args0: [\n // Each arg is associated with a %# in the message.\n // This one gets substituted for %1.\n {\n // The type specifies the kind of input or field to be inserted.\n type: 'field_number',\n // The name allows you to reference the field and get its value.\n name: 'FIELD_NAME',\n }\n ],\n // Adds an untyped previous connection to the top of the block.\n previousStatement: null,\n // Adds an untyped next connection to the bottom of the block.\n nextStatement: null,\n }\n ]);\n\n // Register the definition.\n Blockly.common.defineBlocks(definitions);\n\nFor more information about how to define your blocks and add them to your\ntoolbox, see [Custom blocks overview](/blockly/guides/create-custom-blocks/overview)."]]