Getting Started with Gateway within mins

Getting Started with Gateway within mins

Gateway Protocol is a decentralized infrastructure that enables secure and private data sharing while giving users control over their data. In this technical coding blog, we'll walk through the process of building a decentralized application using the Gateway Protocol SDK. We'll cover the key components and demonstrate how to interact with the protocol using code snippets.

Understanding Data Models:


At the core of Gateway Protocol lies the concept of data models. A data model serves as a blueprint for structuring and organizing personal data. It defines the fields, data types, and validation rules for the information you want to store in a PDA. Gateway Protocol offers a flexible and intuitive way to create custom data models tailored to your specific requirements.

Prerequisites:


Before we begin, make sure you have the following:

  • Node.js installed
  • Gateway Protocol SDK (@gateway-dao/sdk)
  • API key and token from Gateway Protocol's Developer Access Dashboard (keep these protected in environment files)

You can find the entire code with more functions in this GitHub repository.

Setting Up the Gateway Instance:


Before we dive into creating data models and PDAs, let's set up the Gateway instance. The Gateway instance is the entry point for interacting with the Gateway Protocol SDK. Here's how you can configure it:

import { Gateway } from "@gateway-dao/sdk";
import dotenv from "dotenv";

dotenv.config();

export const gatewayInstance = new Gateway({
  url: "https://sandbox.protocol.mygateway.xyz/graphql",
  apiKey: process.env.API_KEY || "YOUR_API_KEY",
  token: process.env.TOKEN || "YOUR_TOKEN",
});

In this code snippet, we import the necessary dependencies, including the Gateway SDK and the dotenv package for handling environment variables. We then create a new instance of the Gateway class, specifying the URL of the Gateway Protocol GraphQL endpoint and providing the API key and token. Make sure to replace "YOUR_API_KEY" and "YOUR_TOKEN" with your actual API key and token, or store them securely in environment variables.

Creating a Data Model:


To get started, you can either choose from a wide range of pre-existing data models available in the Gateway Protocol Explorer or create your own. Here's a code snippet that demonstrates how to create a custom data model using the Gateway Protocol SDK:

import { gatewayInstance } from "../GatewayInstance";

export async function createDataModel() {
  try {
    const { createDataModel } = await gatewayInstance.dataModel.createDataModel({
      title: "Your Data Model Title",
      description: "A brief description of your data model.",
      permissions: "ALL",
      tags: ["Tag1", "Tag2"],
      schema: {
        type: "object",
        default: {},
        title: "Your Data Model Title",
        required: ["field1", "field2", "field3"],
        properties: {
          field1: {
            type: "string",
            title: "Field 1",
          },
          field2: {
            type: "number",
            title: "Field 2",
            default: 0,
          },
          field3: {
            type: "boolean",
            title: "Field 3",
            default: false,
          },
        },
      },
    });
    console.log("Data model created:", createDataModel);
    console.log("Your Data model ID:", createDataModel.id);
    return createDataModel;
  } catch (error) {
    console.error("Error creating data model:", error);
    throw error;
  }
}

In this code snippet, we define the createDataModel function that interacts with the Gateway Protocol SDK to create a new data model. You can specify the title, description, permissions, tags, and schema for your data model. The schema defines the structure and validation rules for the data fields.

Here is a diagram that explains the flow forward.

Process Flow

Creating Personal Data Assets (PDAs):


Once you have a data model ready, creating PDAs becomes a breeze. PDAs are instances of data that conform to the structure defined by the data model. Here's how you can create a PDA using the Gateway Protocol SDK:

import { gatewayInstance } from "../GatewayInstance";
import { PDAStatus, UserIdentifierType } from "@gateway-dao/sdk";

export async function createPDA() {
  let obj = {
    dataModelId: "9f27397e-27f2-4c30-b1b7-829371de4df5",
    description: "Description of the PDA",
    title: "Favorite Person on Crypto Twitter",
    claim: {
      handleName: "@gateway_xyz",
      favoritePosts: ["awesome"],
    },
    owner: {
      type: UserIdentifierType.GATEWAY_ID,
      value: "saviour1002",
    },
  };
  const { createPDA } = await gatewayInstance.pda.createPDA(obj);
  console.log(createPDA);
}

In this code snippet, we define the createPDA function that utilizes the Gateway Protocol SDK to create a new PDA.

You need to provide the dataModelId of the data model you want to use, along with other relevant information such as the description, title, claim data, and owner details.


Gateway Protocol revolutionizes the way we handle personal data by putting users in the driver's seat. By leveraging the Gateway Protocol SDK, developers can easily integrate secure and private data-sharing capabilities into their applications while giving users control over their data.

By following the steps outlined in this guide, you can start building applications that harness the potential of PDAs in just 5 minutes.

Embrace the future of data ownership and unlock new possibilities with Gateway Protocol. Find out what else you can build on Gateway.

Happy Coding!!

Read more