Version 1.0.0
GitHub
Get Support

Building Your First Function

Overview

This guide will walk you through the process of creating, deploying, and testing your first Function on the Neo Service Layer. By the end of this guide, you'll have a working Function that can be triggered via API and interacts with the Neo N3 blockchain.

Prerequisites

Before getting started, ensure you have:

  • A Neo Service Layer account with API keys
  • A Neo N3 wallet with some GAS tokens
  • Basic knowledge of JavaScript
  • The Neo Service Layer SDK installed (optional for this guide)

Step 1: Create a Basic Function

Let's start by creating a simple Function that returns a greeting message. We'll build on this foundation to add more features.

Function Code

Create a new JavaScript file named hello.js with the following content:

function main(args) {
  // Get the name from the arguments or use "World" as default
  const name = args.name || "World";
  
  // Log something to the function execution logs
  console.log("Function executed with name:", name);
  
  // Return a simple greeting
  return {
    message: `Hello, ${name}!`,
    timestamp: new Date().toISOString()
  };
}

This function takes an optional name parameter and returns a greeting message along with the current timestamp.

Deploying the Function

You can deploy this function either through the web dashboard or using the API.

Option 1: Using the Web Dashboard

  1. Log in to your Neo Service Layer account
  2. Navigate to the Functions section
  3. Click "Create New Function"
  4. Enter "HelloWorld" as the function name
  5. Paste the code from hello.js into the code editor
  6. In the Trigger section, select "API" as the trigger type and ensure "Authentication Required" is checked
  7. Click "Save and Deploy"

Option 2: Using the API

You can also deploy the function programmatically:

// Using the JavaScript SDK
import { NeoServiceLayer } from 'neo-service-layer-sdk';

const serviceLayer = new NeoServiceLayer({
  apiKey: 'YOUR_API_KEY',
  network: 'mainnet', // or 'testnet'
});

async function deployFunction() {
  const functionCode = `function main(args) {
  // Get the name from the arguments or use "World" as default
  const name = args.name || "World";
  
  // Log something to the function execution logs
  console.log("Function executed with name:", name);
  
  // Return a simple greeting
  return {
    message: `Hello, ${name}!`,
    timestamp: new Date().toISOString()
  };
}`;
  
  const config = {
    name: 'HelloWorld',
    trigger: {
      type: 'api',
      authRequired: true
    },
    timeout: 10000 // 10 seconds
  };
  
  try {
    const result = await serviceLayer.functions.deploy(functionCode, config);
    console.log('Function deployed successfully:', result.id);
    return result;
  } catch (error) {
    console.error('Deployment failed:', error);
  }
}

deployFunction();

Was this page helpful?

Edit this page on GitHub