---
# System prepended metadata

title: Stellar SDK examples

---

# Stellar SDK examples

## Create account

```elixir
{source_pk, _sk} =
      source_key_pair =
      Stellar.KeyPair.from_secret_seed("SBXUGZ25PNYMPMK36M52TDPB4HSF4CZ23DJPY4ENZMGAN2D4CYFSSVIL")

# 1. Set the creator account 
source_account = Stellar.TxBuild.Account.new(source_pk)

    # 2. set the next sequence number for the founder account
    {:ok, seq_num} = Stellar.Horizon.Accounts.fetch_next_sequence_number(source_pk)

    sequence_number = Stellar.TxBuild.SequenceNumber.new(seq_num)

    # 3. build the create_account operation
    operation =
      Stellar.TxBuild.CreateAccount.new(
        destination: "GA5LAUWYGVTWLNUKQK42EJ2VHMXN5TEYTUWD4KBHVMOTQWH2IMZUUQLV",
        starting_balance: 2
      )

    # 4. build the tx signatures
    signature = Stellar.TxBuild.Signature.new(source_key_pair)

    # 5. submit the transaction to Horizon
    {:ok, envelope} =
      source_account
      |> Stellar.TxBuild.new(sequence_number: sequence_number)
      |> Stellar.TxBuild.add_operation(operation)
      |> Stellar.TxBuild.sign(signature)
      |> Stellar.TxBuild.envelope()

    Stellar.Horizon.Transactions.create(envelope)
```

## Create payment setting trustline
```elixir
{payer_pk, _sk} =
      payer_key_pair =
      Stellar.KeyPair.from_secret_seed("SCP6HOQYN57RWQHVDPTPDGV3FMTYL3AULW3A26S5XTB2UNMZ52IFL5FN")

    {paid_pk, _sk} =
      paid_key_pair =
      Stellar.KeyPair.from_secret_seed("SAHGVCA37V54AACEYNDLNIB356BWV3WEALMYFOAVX2OE24SZCIBDRO4X")

    # 1. set the payer account
    source_account = Stellar.TxBuild.Account.new(payer_pk)

    # 2. set the next sequence number for the payer account
    {:ok, seq_num} = Stellar.Horizon.Accounts.fetch_next_sequence_number(payer_pk)

    sequence_number = Stellar.TxBuild.SequenceNumber.new(seq_num)

    asset = [code: "MTK", issuer: "GA2YG3YULNTUEMMLN4HUQVL7B37GJTYSRZYH6HZUFLXFDCCGKLXIXMDT"]

    # 3. Set trustline for the payment
    trustline_operation =
      Stellar.TxBuild.ChangeTrust.new(
        asset: asset,
        source_account: paid_pk
      )

    # 4. build the payment operation
    operation =
      Stellar.TxBuild.Payment.new(
        destination: paid_pk,
        asset: asset,
        amount: 1000
      )

    # 5. build the tx signatures
    payer_signature = Stellar.TxBuild.Signature.new(payer_key_pair)
    paid_signature = Stellar.TxBuild.Signature.new(paid_key_pair)

    # 6. submit the transaction to Horizon
    {:ok, envelope} =
      source_account
      |> Stellar.TxBuild.new(sequence_number: sequence_number)
      |> Stellar.TxBuild.add_operation(trustline_operation)
      |> Stellar.TxBuild.add_operation(operation)
      |> Stellar.TxBuild.sign([paid_signature, payer_signature])
      |> Stellar.TxBuild.envelope()

    Stellar.Horizon.Transactions.create(envelope)
```

## Create payment without trustline

```elixir
{payer_pk, _sk} =
      signer_key_pair =
      Stellar.KeyPair.from_secret_seed("SCP6HOQYN57RWQHVDPTPDGV3FMTYL3AULW3A26S5XTB2UNMZ52IFL5FN")

    {paid_pk, _sk} =
      Stellar.KeyPair.from_secret_seed("SAHGVCA37V54AACEYNDLNIB356BWV3WEALMYFOAVX2OE24SZCIBDRO4X")

    # 1. set the payer account
    source_account = Stellar.TxBuild.Account.new(payer_pk)

    # 2. set the next sequence number for the payer account
    {:ok, seq_num} = Stellar.Horizon.Accounts.fetch_next_sequence_number(payer_pk)

    sequence_number = Stellar.TxBuild.SequenceNumber.new(seq_num)

    asset = [code: "MTK", issuer: "GA2YG3YULNTUEMMLN4HUQVL7B37GJTYSRZYH6HZUFLXFDCCGKLXIXMDT"]

    # 3. build the payment operation
    operation =
      Stellar.TxBuild.Payment.new(
        destination: paid_pk,
        asset: asset,
        amount: 1000
      )

    # 4. build the tx signatures
    payer_signature = Stellar.TxBuild.Signature.new(signer_key_pair)

    # 5. submit the transaction to Horizon
    {:ok, envelope} =
      source_account
      |> Stellar.TxBuild.new(sequence_number: sequence_number)
      |> Stellar.TxBuild.add_operation(operation)
      |> Stellar.TxBuild.sign(payer_signature)
      |> Stellar.TxBuild.envelope()

    Stellar.Horizon.Transactions.create(envelope)
```

## Create asset

```elixir
    payer_secret = "SCWQHQGSMYXVEGF3THMKIJMWWFW3ADBVNWRGHL7KA72OQNNJNLSGED25"
    paid_secret = "SBTHIZ7MVKZQXGPMKLJGXJMGB4SVY22PPR7LUYCRG2ZAEY5MIUDKUCDT"

    {issuer_pk, _issuer_sk} = issuer_keypair = Stellar.KeyPair.from_secret_seed(payer_secret)
    {distribution_pk, _sk} = distribution_keypair = Stellar.KeyPair.from_secret_seed(paid_secret)

    source_account = Stellar.TxBuild.Account.new(issuer_pk)

    asset_code = "MTK"
    asset_supply = 1_000_000

    # 2. set the next sequence number for the issuer account
    {:ok, seq_num} = Stellar.Horizon.Accounts.fetch_next_sequence_number(issuer_pk)

    sequence_number = Stellar.TxBuild.SequenceNumber.new(seq_num)

    # 3. build the create_account operation
    asset = [code: asset_code, issuer: issuer_pk]

    trustline_operation =
      Stellar.TxBuild.ChangeTrust.new(
        asset: asset,
        source_account: distribution_pk
      )

    create_payment_operation =
      Stellar.TxBuild.Payment.new(
        destination: distribution_pk,
        asset: asset,
        amount: asset_supply,
        source_account: issuer_pk
      )

    # 4. build the tx signatures
    distribution_signature = Stellar.TxBuild.Signature.new(distribution_keypair)
    issuer_signature = Stellar.TxBuild.Signature.new(issuer_keypair)

    # 5. submit the transaction to Horizon
    {:ok, envelope} =
      source_account
      |> Stellar.TxBuild.new(sequence_number: sequence_number)
      |> Stellar.TxBuild.add_operation(trustline_operation)
      |> Stellar.TxBuild.add_operation(create_payment_operation)
      |> Stellar.TxBuild.sign([issuer_signature, distribution_signature])
      |> Stellar.TxBuild.envelope()

    Stellar.Horizon.Transactions.create(envelope)
```

## Authorize asset

```elixir
{truster_pk, _sk} =
      truster_keypair =
      Stellar.KeyPair.from_secret_seed("SDG3KYI5RR6YKUECA4J6TNOAPY2XW7OFHAPKR3LII4R47W4SYZNPMPY6")

    # 1. Set the truster source account
    source_account = Stellar.TxBuild.Account.new(truster_pk)

    # 2. set the next sequence number for the issuer account
    {:ok, seq_num} = Stellar.Horizon.Accounts.fetch_next_sequence_number(truster_pk)
    sequence_number = Stellar.TxBuild.SequenceNumber.new(seq_num)

    # 3. build the trustline operation
    trustline_operation =
      Stellar.TxBuild.ChangeTrust.new(
        asset: [code: "MTK", issuer: "GA2YG3YULNTUEMMLN4HUQVL7B37GJTYSRZYH6HZUFLXFDCCGKLXIXMDT"]
      )

    # 4. build the tx signatures
    truster_signature = Stellar.TxBuild.Signature.new(truster_keypair)

    # 5. submit the transaction to Horizon
    {:ok, envelope} =
      source_account
      |> Stellar.TxBuild.new(sequence_number: sequence_number)
      |> Stellar.TxBuild.add_operation(trustline_operation)
      |> Stellar.TxBuild.sign(truster_signature)
      |> Stellar.TxBuild.envelope()

    Stellar.Horizon.Transactions.create(envelope)
```