> ## Documentation Index
> Fetch the complete documentation index at: https://plivo.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Started with Java

> Send your first SMS using Java and the Spring framework with Plivo APIs

<Tabs>
  <Tab title="Java & Spring">
    # Get Started with Java Using the Spring Framework

    ## Sign up for a Plivo account

    When you [sign up with Plivo](https://cx.plivo.com/signup), we give you a free trial account and free credits to experiment with and learn about our services. You can [add a number](https://cx.plivo.com/phone-numbers) to your account to start testing the full range of our voice and SMS service features.

    Follow these steps to get a free trial account:

    1. [Sign up](https://cx.plivo.com/signup) with your work email address.
    2. Check your inbox for an activation email message from Plivo. Click on the link in the message to activate your account.
    3. Enter your mobile number to complete the phone verification step.

    ### Sign up with your work email address

    <Frame>
      <img src="https://mintcdn.com/plivo/Ac6PoKJHHxDx1U63/images/signup.png?fit=max&auto=format&n=Ac6PoKJHHxDx1U63&q=85&s=56190e6fffc6ae0c70dba8c7c69f435b" width="1440" height="900" data-path="images/signup.png" />
    </Frame>

    If you have any issues creating a Plivo account, please contact our [support team](https://support.plivo.com/hc/en-us/) for assistance.

    To get started, try sending an SMS message either by using our API and XML documents, or via {/* [PHLO](/phlo/) */}, our visual design tool, which allows you to create message flows using an intuitive canvas and deploy them with few clicks.

    <Tabs>
      <Tab title="Using API">
        ## Install Java, Spring, and the Plivo Java SDK

        You must set up and install Java 1.8 or higher, Spring, and Plivo’s Java SDK before you send your first SMS message.

        ### Install Java

        You can check your Java version under macOS or Linux by running the command `java -version` in a terminal window. Under Windows there are [several ways to check](https://www.java.com/en/download/help/version_manual.html). If you don’t have Java installed or need a more current version, <a href="https://www.oracle.com/java/technologies/downloads/">download and install it</a>.

        You should also [download and install IntelliJ Idea](https://www.jetbrains.com/idea/download/).

        ### Create a Spring application

        Use [Spring Initializr](https://start.spring.io/) to create a boilerplate project with the Spring Boot framework.

        <Frame>
          <img src="https://mintcdn.com/plivo/NFI9_HRHTMInDf93/images/boilerplate.png?fit=max&auto=format&n=NFI9_HRHTMInDf93&q=85&s=e331fc3ecf450c83024d9dc2fcc7f4e5" alt="Create Boilerplate code" width="1440" height="788" data-path="images/boilerplate.png" />
        </Frame>

        Choose the `Spring Web` dependency. Give the project a friendly name — we used `Plivo SMS` — and click **Generate** to download boilerplate code, which will be named PlivoSmsApplication.java based on the friendly name we supplied. Open it in IntelliJ Idea.

        <Frame>
          <img src="https://mintcdn.com/plivo/VVpGlua-mdfO5CNz/images/intellijboilerplate.png?fit=max&auto=format&n=VVpGlua-mdfO5CNz&q=85&s=78c8accbf1ba8693f7cada0c1c11c5ce" alt="Boilerplate project in IntelliJ" width="1440" height="811" data-path="images/intellijboilerplate.png" />
        </Frame>

        <Note>
          <strong>Note:</strong> Set the Java target as 11.
        </Note>

        ### Install the Plivo Java SDK using IntelliJ Idea

        Install the Plivo Java SDK by adding the dependency in `pom.xml`.

        ```xml theme={null}
            <dependency>
                <groupId>com.plivo</groupId>
                <artifactId>plivo-java</artifactId>
               <version>5.9.3</version>
            </dependency>
        ```

        <Frame>
          <img src="https://mintcdn.com/plivo/-VVFcM3g7XHd8wTl/images/plivo-java.png?fit=max&auto=format&n=-VVFcM3g7XHd8wTl&q=85&s=90a059a185deec3e4b3ed1f28cc56362" alt="Install Plivo package" width="1439" height="811" data-path="images/plivo-java.png" />
        </Frame>

        Once you’ve set up your development environment, you can start sending and receiving messages using our APIs and XML documents. Here are three common use cases to get you started.

        ## Send your first outbound SMS/MMS message

        You must have a Plivo phone number to send messages to the US or Canada; you can rent a Plivo number from Phone Numbers > [Buy Numbers](https://cx.plivo.com/phone-numbers) on the Plivo console or via the [Numbers API](/numbers/api/phone-number/#buy-a-phone-number).

        ### Edit the Spring application

        Edit the PlivoSmsApplication.java file in the src/main/java/com.example.demo/ folder and paste into it this code.

        <Tabs>
          <Tab title="SMS">
            ```java theme={null}
            package com.example.demo;
            import com.plivo.api.Plivo;
            import com.plivo.api.exceptions.PlivoRestException;
            import com.plivo.api.models.message.Message;
            import com.plivo.api.models.message.MessageCreateResponse;
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.web.bind.annotation.GetMapping;
            import org.springframework.web.bind.annotation.RestController;
            import java.io.IOException;
            import java.util.Collections;

            @SpringBootApplication
            @RestController
            public class PlivoSmsApplication {


                public static void main(String[] args) {
                    SpringApplication.run(PlivoSmsApplication.class, args);
                }

                @GetMapping(value = "/outbound", produces = {"application/json"})
                public MessageCreateResponse sendSMS() throws IOException, PlivoRestException {
                    Plivo.init("<auth_id>", "<auth_token>");
                    MessageCreateResponse response = Message.creator(
                       "<sender_id>", 
                       "<destination_number>",
                       "Hello, from Spring!").create();
                    System.out.println(response);
                    return response;
                }
            }
            ```
          </Tab>

          <Tab title="MMS">
            ```java theme={null}
            package com.example.demo;
            import com.plivo.api.Plivo;
            import com.plivo.api.exceptions.PlivoRestException;
            import com.plivo.api.models.message.Message;
            import com.plivo.api.models.message.MessageCreateResponse;
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.web.bind.annotation.GetMapping;
            import org.springframework.web.bind.annotation.RestController;
            import java.io.IOException;
            import java.util.Collections;

            @SpringBootApplication
            @RestController
            public class PlivoSmsApplication {

                public static void main(String[] args) {
                    SpringApplication.run(PlivoSmsApplication.class, args);
                }

                @GetMapping(value = "/outbound", produces = {"application/json"})
                public MessageCreateResponse sendMMS() throws IOException, PlivoRestException {
                    Plivo.init("<auth_id>", "<auth_token>");
                    MessageCreateResponse response = Message.creator(
                       "<sender_id>", 
                       "<destination_number>",
                       "Hello, from Spring!")
                       .type(MessageType.MMS)
                       .media_urls(new String[]{"https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif"})
                       .media_ids(new String[]{"801c2056-33ab-499c-80ef-58b574a462a2"}).create();
                    System.out.println(response);
                    return response;
                }
            }
            ```
          </Tab>
        </Tabs>

        Replace the auth placeholders with your authentication credentials from the [Plivo console](https://cx.plivo.com/home). Replace the phone number placeholders with actual phone numbers in [E.164 format](https://en.wikipedia.org/wiki/E.164) (for example, +12025551234). In countries other than the US and Canada you can use a [sender ID](/messaging/concepts/sender-id-usage/) for the message source.

        <Note>
          <strong>Note:</strong>
          We recommend that you store your credentials in the `auth_id` and `auth_token` environment variables to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use `System.getenv()` to store environment variables and fetch them when initializing the client.
        </Note>

        Save the file and run it.

        <Frame>
          <img src="https://mintcdn.com/plivo/sqGJ0ONkT5kTuesy/images/send-sms.png?fit=max&auto=format&n=sqGJ0ONkT5kTuesy&q=85&s=dc419fce02006165616c628d59adef61" alt="Send SMS" width="1439" height="809" data-path="images/send-sms.png" />
        </Frame>

        ## Receive your first inbound SMS/MMS message

        To receive incoming messages, you must have a Plivo phone number that supports SMS; you can rent numbers from the [Numbers](https://cx.plivo.com/phone-numbers) page of the Plivo console or by using the [Numbers API](/numbers/).

        ### Edit the Spring application

        Edit PlivoSmsApplication.java file in the src/main/java/com.example.demo/ folder and paste into it this code after the `sendSMS` function block.

        <Tabs>
          <Tab title="SMS">
            ```java theme={null}
            package com.example.demo;
            import com.plivo.api.exceptions.PlivoRestException;
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.web.bind.annotation.GetMapping;
            import org.springframework.web.bind.annotation.RequestParam;
            import org.springframework.web.bind.annotation.RestController;
            import java.io.IOException;

            @SpringBootApplication
            @RestController
            public class PlivoSmsApplication {


                public static void main(String[] args) {
                    SpringApplication.run(PlivoSmsApplication.class, args);
                }

                @GetMapping(value = "/outbound", produces = {"application/json"})
                public MessageCreateResponse SendSMS() throws IOException, PlivoRestException {
                    ........;
                    ........;
                }

                @PostMapping("/incoming")
                public String postBody(String From, String To, String Text) {
                    System.out.println(From + " " + To + " " + Text);
            		return "Message received!";
                }

            }
            ```
          </Tab>

          <Tab title="MMS">
            ```java theme={null}
            package com.example.demo;
            import com.plivo.api.exceptions.PlivoRestException;
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.web.bind.annotation.GetMapping;
            import org.springframework.web.bind.annotation.RequestParam;
            import org.springframework.web.bind.annotation.RestController;
            import java.io.IOException;

            @SpringBootApplication
            @RestController
            public class PlivoSmsApplication {


                public static void main(String[] args) {
                    SpringApplication.run(PlivoSmsApplication.class, args);
                }

                @GetMapping(value = "/outbound", produces = {"application/json"})
                public MessageCreateResponse SendSMS() throws IOException, PlivoRestException {
                    ........;
                    ........;
                }

                @PostMapping("/incoming")
                public String postBody(String From, String To, String Text, String Media0) {
                    System.out.println(From + " " + To + " " + Text + " " + Media0);
            		return "Message received!";
                }

            }
            ```
          </Tab>
        </Tabs>

        <Note>
          <strong>Note:</strong> Update the import declaration section as well.
        </Note>

        <Frame>
          <img src="https://mintcdn.com/plivo/7-odxN9fJG_Dg1dt/images/receive-sms.png?fit=max&auto=format&n=7-odxN9fJG_Dg1dt&q=85&s=59d6ea6afd8c9b16bba8c6f1e6f02b72" alt="Receive SMS" width="2048" height="1168" data-path="images/receive-sms.png" />
        </Frame>

        Run the project and you should see your basic server application in action at [http://localhost:8080/incoming/](http://localhost:8080/incoming/).

        ### Expose your local server to the internet

        To receive incoming messages, your local server must connect with Plivo API services. For that, we recommend using [ngrok](https://ngrok.com/download), which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.

        <Frame>
          <img src="https://mintcdn.com/plivo/M2NzHE_bNZbCm0gd/images/ngrok-diagram.png?fit=max&auto=format&n=M2NzHE_bNZbCm0gd&q=85&s=b2ef4b4d52c133d126fd0d4303c33a61" alt="ngrok block diagram" width="1626" height="601" data-path="images/ngrok-diagram.png" />
        </Frame>

        Install ngrok and run it on the command line, specifying the port that  hosts the application on which you want to receive messages (80 in this case):

        ```shell theme={null}
        $ ./ngrok http 80
        ```

        This starts the ngrok server on your local server. Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

        <Frame>
          <img src="https://mintcdn.com/plivo/M2NzHE_bNZbCm0gd/images/ngrok.png?fit=max&auto=format&n=M2NzHE_bNZbCm0gd&q=85&s=ce35ccb7eebdb2af565a2a3a2cf2be18" alt="Sample ngrok CLI" width="786" height="238" data-path="images/ngrok.png" />
        </Frame>

        Now people can send messages to your Plivo number.

        ### Create a Plivo application to receive messages

        Associate the controller you created with Plivo by creating a Plivo application. Visiting Messaging > [Applications](https://cx.plivo.com/xml-applications) and click **Add New Application**. You can also use Plivo's [Application API](/account/api/application/#create-an-application).

        Give your application a name — we called ours `Receive SMS`. Enter the server URL you want to use (for example `https://<yourdomain>.com/receive_sms/`) in the `Message URL` field and set the method to `POST`. Click **Create Application** to save your application.

        <Frame>
          <img src="https://mintcdn.com/plivo/2OFvQXVNT3srKLUy/images/create_SMS_app.jpg?fit=max&auto=format&n=2OFvQXVNT3srKLUy&q=85&s=665006f2112a8e6113716029adfbf2a7" alt="Create Application" width="1440" height="785" data-path="images/create_SMS_app.jpg" />
        </Frame>

        ### Assign a Plivo number to your application

        Navigate to the [Numbers](https://cx.plivo.com/phone-numbers) page and select the phone number you want to use for this application.

        From the Application Type drop-down, select `XML Application`.

        From the Plivo Application drop-down, select `Receive SMS` (the name we gave the application).

        Click **Update Number** to save.

        <Frame>
          <img src="https://mintcdn.com/plivo/NFI9_HRHTMInDf93/images/assign_SMS_app.jpg?fit=max&auto=format&n=NFI9_HRHTMInDf93&q=85&s=94324dc41c7e8ade7a60c98e5fe20094" alt="Assign Phone Number to Receive SMS App" width="1440" height="785" data-path="images/assign_SMS_app.jpg" />
        </Frame>

        ### Test

        Send a text message to the Plivo number you specified using any phone.

        ## Reply to an incoming SMS/MMS message

        To receive incoming messages, you must have a Plivo phone number that supports SMS; you can rent numbers from the [Numbers](https://cx.plivo.com/phone-numbers) page of the Plivo console or by using the [Numbers API](/numbers/).

        ### Edit the Spring application

        Edit PlivoSMSApplication.java in the src/main/java/com.example.demo/ folder and paste into it this code after the `incoming sms` function block.

        ```java theme={null}
        package com.example.demo;
        import com.plivo.api.exceptions.PlivoRestException;
        import com.plivo.api.xml.Message;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RestController;
        import com.plivo.api.exceptions.PlivoXmlException;
        import com.plivo.api.xml.Response;

        import java.io.IOException;

        @SpringBootApplication
        @RestController
        public class PlivoSmsApplication {


            public static void main(String[] args) {
                SpringApplication.run(PlivoSmsApplication.class, args);
            }
            
            // Send an outbound SMS
            @GetMapping(value = "/outbound", produces = {"application/json"})
            public MessageCreateResponse SendSMS() throws IOException, PlivoRestException {
                ........;
                ........;
            }
            
            // Receive incoming SMS
            @PostMapping("/incoming")
            public String postBody(String From, String To, String Text) {
                ........;
                ........;
            }
            
            // Reply to incoming SMS
            @GetMapping(value = "/reply", produces = {"application/xml"})
            public String getBody(String From, String To, String Text) throws PlivoXmlException {
                Response res = new Response().children(
                    new Message(To, From, "This is an automatic response"));
                return res.toXmlString();
            }

        }
        ```

        If you haven’t done so already, [expose your local server to the internet](/sdk/server/set-up-java-dev-environment-api-messaging/#ngrok-setup).

        ### Create a Plivo application to reply to messages

        Associate the controller you created with Plivo by creating a Plivo application. Visiting Messaging > [Applications](https://cx.plivo.com/xml-applications) and click **Add New Application**. You can also use Plivo's [Application API](/account/api/application/#create-an-application).

        Give your application a name — we called ours `Reply Incoming SMS`. Enter the server URL you want to use (for example `http://<yourdomain>.com/replysms/`) in the `Message URL` field and set the method to `POST`. Click **Create Application** to save your application.

        <Frame>
          <img src="https://mintcdn.com/plivo/2OFvQXVNT3srKLUy/images/create_reply_sms.png?fit=max&auto=format&n=2OFvQXVNT3srKLUy&q=85&s=0371a975aa748267124e9ea9debc7a0f" alt="Create Application" width="1440" height="822" data-path="images/create_reply_sms.png" />
        </Frame>

        ### Assign a Plivo number to your application

        Navigate to the [Numbers](https://cx.plivo.com/phone-numbers) page and select the phone number you want to use for this application.

        From the Application Type drop-down, select `XML Application`.

        From the Plivo Application drop-down, select `Reply Incoming SMS` (the name we gave the application).

        Click **Update Number** to save.

        ### Test

        Send a text message to the Plivo number you specified using any phone. You should receive a reply.

        ## More use cases

        We illustrate [more than a dozen use cases](/messaging/use-cases/send-an-sms/java/) with code for both API/XML and PHLO on our documentation pages.
      </Tab>
    </Tabs>
  </Tab>

  <Tab title="Java & Spark">
    # Get Started with Java Using the Spark Framework

    ## Sign up for a Plivo account

    When you [sign up with Plivo](https://cx.plivo.com/signup), we give you a free trial account and free credits to experiment with and learn about our services. You can [add a number](https://cx.plivo.com/phone-numbers) to your account to start testing the full range of our voice and SMS service features.

    Follow these steps to get a free trial account:

    1. [Sign up](https://cx.plivo.com/signup) with your work email address.
    2. Check your inbox for an activation email message from Plivo. Click on the link in the message to activate your account.
    3. Enter your mobile number to complete the phone verification step.

    ### Sign up with your work email address

    <Frame>
      <img src="https://mintcdn.com/plivo/Ac6PoKJHHxDx1U63/images/signup.png?fit=max&auto=format&n=Ac6PoKJHHxDx1U63&q=85&s=56190e6fffc6ae0c70dba8c7c69f435b" width="1440" height="900" data-path="images/signup.png" />
    </Frame>

    If you have any issues creating a Plivo account, please contact our [support team](https://support.plivo.com/hc/en-us/) for assistance.

    To get started, try sending an SMS message either by using our API and XML documents, or via {/* [PHLO](/phlo/) */}, our visual design tool, which allows you to create message flows using an intuitive canvas and deploy them with few clicks.

    <Tabs>
      <Tab title="Using API">
        ## Install Java and the Plivo Java SDK

        You must set up and install Java 1.8 or higher and Plivo’s Java SDK before you send your first message.

        ### Install Java

        You can check your Java version under macOS or Linux by running the command `java -version` in a terminal window. Under Windows there are [several ways to check](https://www.java.com/en/download/help/version_manual.html). If you don’t have Java installed or need a more current version, <a href="https://www.oracle.com/java/technologies/downloads/">download and install it</a>.

        You should also [download and install IntelliJ Idea](https://www.jetbrains.com/idea/download/).

        ### Install the Plivo Java SDK using IntelliJ Idea

        Create a new project in IntelliJ Idea.

        <Frame>
          <img src="https://mintcdn.com/plivo/M2NzHE_bNZbCm0gd/images/java/step1.png?fit=max&auto=format&n=M2NzHE_bNZbCm0gd&q=85&s=e17818608bd9d132b7b565a919cb0f47" alt="Create New Project" width="1127" height="532" data-path="images/java/step1.png" />
        </Frame>

        Choose a dependency management tool and Java SE SDK for the new project.

        <Frame>
          <img src="https://mintcdn.com/plivo/M2NzHE_bNZbCm0gd/images/java/step2.png?fit=max&auto=format&n=M2NzHE_bNZbCm0gd&q=85&s=88ab5cbbdde173a5d0ab29fdd11fc3d2" alt="Choose Dependency management" width="1272" height="716" data-path="images/java/step2.png" />
        </Frame>

        ### Install the Plivo Java SDK using IntelliJ Idea

        * Install the Plivo Java, Spark & SLF4j package by adding the dependency in `pom.xml`

        ```xml theme={null}
        <dependencies>
           <dependency>
              <groupId>com.sparkjava</groupId>
              <artifactId>spark-core</artifactId>
              <version>2.9.1</version>
           </dependency>
           <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-simple</artifactId>
              <version>1.7.21</version>
           </dependency>
           <dependency>
              <groupId>com.plivo</groupId>
              <artifactId>plivo-java</artifactId>
              <version>5.9.3</version>
           </dependency>
        </dependencies>
        ```

        <Frame>
          <img src="https://mintcdn.com/plivo/sqGJ0ONkT5kTuesy/images/spark.png?fit=max&auto=format&n=sqGJ0ONkT5kTuesy&q=85&s=f0c8a18c230d64d18d9308665a8333d0" alt="Install package" width="1440" height="900" data-path="images/spark.png" />
        </Frame>

        Once you’ve set up your development environment, you can start sending and receiving messages using our APIs and XML documents. Here are three common use cases to get you started.

        ## Send your first outbound SMS/MMS message

        You must have a Plivo phone number to send messages to the US or Canada; you can rent a Plivo number from Phone Numbers > [Buy Numbers](https://cx.plivo.com/phone-numbers) on the Plivo console or via the [Numbers API](/numbers/api/phone-number/#buy-a-phone-number).

        ### Create a Java class

        Create a Java class in the project called `SendSMS` and paste into it this code.

        <Tabs>
          <Tab title="SMS">
            ```java theme={null}
            package com.plivo.api.samples.sms;

            import com.plivo.api.Plivo;
            import com.plivo.api.exceptions.PlivoRestException;
            import com.plivo.api.models.message.Message;
            import com.plivo.api.models.message.MessageCreateResponse;

            import java.io.IOException;
            import java.util.Collections;

            import static spark.Spark.*;

            class SendSMS {
                public static void main(String[] args) {
                    get("/outbound_sms", (req, resp) -> {
                        Plivo.init("<auth_id>", "<auth_token>");
                        try {
                            MessageCreateResponse response = Message.creator(
                                    "<sender_id>",
                                    Collections.singletonList("<destination_number>"),
                                    "Hello, from Spark!").create();
                            return response;
                        } catch (PlivoRestException | IOException exception) {
                            return exception;
                        } });
                }
            }
            ```
          </Tab>

          <Tab title="MMS">
            ```java theme={null}
            package com.plivo.api.samples.sms;

            import com.plivo.api.Plivo;
            import com.plivo.api.exceptions.PlivoRestException;
            import com.plivo.api.models.message.Message;
            import com.plivo.api.models.message.MessageCreateResponse;
            import com.plivo.api.models.message.MessageType;

            import java.io.IOException;
            import java.util.Collections;

            import static spark.Spark.*;

            class SendSMS {
                public static void main(String[] args) {
                    get("/outbound_mms", (req, resp) -> {
                        Plivo.init("<auth_id>", "<auth_token>");
                        try {
                            MessageCreateResponse response = Message.creator(
                                    "<sender_id>",
                                    Collections.singletonList("<destination_number>"),
                                    "Hello, MMS from Spark!")
                                    .type(MessageType.MMS)
                                    .media_urls(new String[]{"https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif"})
                                    .media_ids(new String[]{"801c2056-33ab-499c-80ef-58b574a462a2"}).create();
                            return response;
                        } catch (PlivoRestException | IOException exception) {
                            return exception;
                        } });
                }
            }
            ```
          </Tab>
        </Tabs>

        Replace the auth placeholders with your authentication credentials from the [Plivo console](https://cx.plivo.com/home). Replace the phone number placeholders with actual phone numbers in [E.164 format](https://en.wikipedia.org/wiki/E.164) (for example, +12025551234). In countries other than the US and Canada you can use a [sender ID](/messaging/concepts/sender-id-usage/) for the message source.

        <Note>
          <strong>Note:</strong>
          We recommend that you store your credentials in the `auth_id` and `auth_token` environment variables to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use `System.getenv()` to store environment variables and fetch them when initializing the client.
        </Note>

        ### Test

        Save the file and run it.

        <Frame>
          <img src="https://mintcdn.com/plivo/sqGJ0ONkT5kTuesy/images/sendsms.png?fit=max&auto=format&n=sqGJ0ONkT5kTuesy&q=85&s=998f836835bdd102e92aef7867568565" alt="Send SMS" width="1440" height="500" data-path="images/sendsms.png" />
        </Frame>

        ## Receive your first inbound SMS/MMS message

        To receive incoming messages, you must have a Plivo phone number that supports SMS; you can rent numbers from the [Numbers](https://cx.plivo.com/phone-numbers) page of the Plivo console or by using the [Numbers API](/numbers/).

        ### Create a web application to handle inbound messages

        Create a Java class named `ReceiveSMS` and paste into it this code.

        <Tabs>
          <Tab title="SMS">
            ```java theme={null}
            import static spark.Spark.*;

            public class ReceiveSms {
                public static void main(String[] args) {
                    get("/receive_sms", (request, response) -> {
                        String from_number = request.queryParams("From");
                        String to_number = request.queryParams("To");
                        String text = request.queryParams("Text");
                        System.out.println(from_number + " " + to_number + " " + text);
                        return "Message Received";
                    });
                }
            }
            ```
          </Tab>

          <Tab title="MMS">
            ```java theme={null}
            import static spark.Spark.*;

            public class ReceiveSms {
                public static void main(String[] args) {
                    get("/receive_mms", (request, response) -> {
                        String from_number = request.queryParams("From");
                        String to_number = request.queryParams("To");
                        String text = request.queryParams("Text");
                        String media_url = request.queryParams("Media)");
                        System.out.println(from_number + " " + to_number + " " + text+" "+ media_url);
                        return "Message Received";
                    });
                }
            }
            ```
          </Tab>
        </Tabs>

        <Frame>
          <img src="https://mintlify.s3.us-west-1.amazonaws.com/plivo/images/receivesms.png" alt="Receive SMS" />
        </Frame>

        Run the project and you should see your basic server application in action at [http://localhost:4567/receive\_sms](http://localhost:4567/receive_sms).

        ### Expose your local server to the internet

        To receive incoming messages, your local server must connect with Plivo API services. For that, we recommend using [ngrok](https://ngrok.com/download), which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.

        <Frame>
          <img src="https://mintcdn.com/plivo/M2NzHE_bNZbCm0gd/images/ngrok-diagram.png?fit=max&auto=format&n=M2NzHE_bNZbCm0gd&q=85&s=b2ef4b4d52c133d126fd0d4303c33a61" alt="ngrok block diagram" width="1626" height="601" data-path="images/ngrok-diagram.png" />
        </Frame>

        Install ngrok and run it on the command line, specifying the port that  hosts the application on which you want to receive messages (80 in this case):

        ```shell theme={null}
        $ ./ngrok http 80
        ```

        This starts the ngrok server on your local server. Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

        <Frame>
          <img src="https://mintcdn.com/plivo/M2NzHE_bNZbCm0gd/images/ngrok.png?fit=max&auto=format&n=M2NzHE_bNZbCm0gd&q=85&s=ce35ccb7eebdb2af565a2a3a2cf2be18" alt="Sample ngrok CLI" width="786" height="238" data-path="images/ngrok.png" />
        </Frame>

        Now people can send messages to your Plivo number.

        ### Create a Plivo application to receive messages

        Associate the controller you created with Plivo by creating a Plivo application. Visiting Messaging > [Applications](https://cx.plivo.com/xml-applications) and click **Add New Application**. You can also use Plivo's [Application API](/account/api/application/#create-an-application).

        Give your application a name — we called ours `Receive SMS`. Enter the server URL you want to use (for example `https://<yourdomain>.com/receive_sms/`) in the `Message URL` field and set the method to `POST`. Click **Create Application** to save your application.

        <Frame>
          <img src="https://mintcdn.com/plivo/2OFvQXVNT3srKLUy/images/create_SMS_app.jpg?fit=max&auto=format&n=2OFvQXVNT3srKLUy&q=85&s=665006f2112a8e6113716029adfbf2a7" alt="Create Application" width="1440" height="785" data-path="images/create_SMS_app.jpg" />
        </Frame>

        ### Assign a Plivo number to your application

        Navigate to the [Numbers](https://cx.plivo.com/phone-numbers) page and select the phone number you want to use for this application.

        From the Application Type drop-down, select `XML Application`.

        From the Plivo Application drop-down, select `Receive SMS` (the name we gave the application).

        Click **Update Number** to save.

        <Frame>
          <img src="https://mintcdn.com/plivo/NFI9_HRHTMInDf93/images/assign_SMS_app.jpg?fit=max&auto=format&n=NFI9_HRHTMInDf93&q=85&s=94324dc41c7e8ade7a60c98e5fe20094" alt="Assign Phone Number to Receive SMS App" width="1440" height="785" data-path="images/assign_SMS_app.jpg" />
        </Frame>

        ### Test

        Send a text message to the Plivo number you specified using any phone.

        ## Reply to an incoming SMS/MMS message

        To receive incoming messages, you must have a Plivo phone number that supports SMS; you can rent numbers from the [Numbers](https://cx.plivo.com/phone-numbers) page of the Plivo console or by using the [Numbers API](/numbers/).

        ### Create a Java class

        Create a Java class named `ReplytoInbound` and paste into it this code.

        ```java theme={null}
        import com.plivo.api.xml.Message;
        import com.plivo.api.xml.Response;
        import static spark.Spark.*;

        class ReplytoInbound {
            public static void main(String[] args) {
                post("/reply_to_inbound/", (request, response) -> {
                    String from_number = request.queryParams("From");
                    String to_number = request.queryParams("To");
                    String text = request.queryParams("Text");
                    System.out.println(from_number + " " + to_number + " " + text);
                    response.type("application/xml");
                    Response resp = new Response()
                            .children(
                                new Message(to_number, from_number, "This is an automatic response")
                                        .type("sms")
                            );
                    return resp.toXmlString();
                });
            }
        }
        ```

        If you haven’t done so already, [expose your local server to the internet](/sdk/server/set-up-java-dev-environment-api-messaging/#ngrok-setup).

        ### Create a Plivo application to reply to messages

        Associate the controller you created with Plivo by creating a Plivo application. Visiting Messaging > [Applications](https://cx.plivo.com/xml-applications) and click **Add New Application**. You can also use Plivo's [Application API](/account/api/application/#create-an-application).

        Give your application a name — we called ours `Reply Incoming SMS`. Enter the server URL you want to use (for example `http://<yourdomain>.com/replysms/`) in the `Message URL` field and set the method to `POST`. Click **Create Application** to save your application.

        <Frame>
          <img src="https://mintcdn.com/plivo/2OFvQXVNT3srKLUy/images/create_reply_sms.png?fit=max&auto=format&n=2OFvQXVNT3srKLUy&q=85&s=0371a975aa748267124e9ea9debc7a0f" alt="Create Application" width="1440" height="822" data-path="images/create_reply_sms.png" />
        </Frame>

        ### Assign a Plivo number to your application

        Navigate to the [Numbers](https://cx.plivo.com/phone-numbers) page and select the phone number you want to use for this application.

        From the Application Type drop-down, select `XML Application`.

        From the Plivo Application drop-down, select `Reply Incoming SMS` (the name we gave the application).

        Click **Update Number** to save.

        ### Test

        Send a text message to the Plivo number you specified using any phone. You should receive a reply.

        ## More use cases

        We illustrate [more than a dozen use cases](/messaging/use-cases/send-an-sms/java/) with code for both API/XML and PHLO on our documentation pages.
      </Tab>
    </Tabs>
  </Tab>
</Tabs>
