# Initialization

{% hint style="success" %}
**Information**

You can find a sample repo on GitHub that's using this library, [here](https://github.com/walkintheparkcoder/microservice/tree/main/test).
{% endhint %}

You can really see this as passing JSON object between your server and your microservice, but instead of passing raw JSON, we can serialize and deserialize it into useful objects actually used in your code.

### Initialization of the API

Here an example of how to initialize the API on Spigot and how to setup your Node.js environment.

{% tabs %}
{% tab title="Spigot Initialization (Client)" %}

### Parameter definitions

<table><thead><tr><th width="162">Parameter</th><th width="125.66666666666666">Type</th><th width="172">Default</th><th>Description</th></tr></thead><tbody><tr><td><strong>.host()</strong></td><td><em>String</em></td><td><code>localhost</code></td><td>The microservice host</td></tr><tr><td><strong>.route()</strong></td><td><em>String</em></td><td><code>'/api'</code></td><td>The main API route</td></tr><tr><td><strong>.port()</strong></td><td><em>int</em></td><td><code>8080</code></td><td>The microservice port</td></tr><tr><td><strong>.protocol</strong></td><td><em>Protocol</em></td><td><code>Protocol.HTTP</code></td><td>The HTTP protocol</td></tr></tbody></table>

#### Creating a new Microservice object

To create a object, we use the *ServiceBuilder* class.

```java
/**
 * Initialize it with default values like this;
 */
new ServiceBuilder().build();

/**
 * Initialize it with custom values like this;
 */
new ServiceBuilder()
   /* To change default values, add .port(), .()... here. */
   .host("customhost.com")
   .protocol(Protocol.HTTPS)
   .build();
```

Establish the host of the API in your Java plugin, which will be utilized for requests.

{% code title="Plugin.java" %}

```java
import micro.api.*;

public final class Plugin extends JavaPlugin {
   public ServiceHost host;
   public PlayerRepository repo;
   
   @Override
   public void onEnable() {
      /**
       * This gives `http://localhost:8080/api` according
       * to the default values stated here above.
       */
       host = new ServiceBuilder().build().getHost();
       repo = new PlayerRepository(host);
   }
}
```

{% endcode %}
{% endtab %}

{% tab title="Node.js Initialization (Host)" %}
**Step 1:** Install the Microservice package from NPM:

```bash
npm install @spigotmc/microservice
```

Or if you're using Yarn:

```bash
yarn add @spigotmc/microservice
```

**Step 2:** Initialize a new microservice. Also, don't forget to add your API route.

{% code title="index.ts" %}

```typescript
import Microservice from "@spigotmc/microservice";
import route from "./routes";

/**
 * MySQL credentials
 */
const mysql = {
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: 'password',
  database: 'minecraft',
};

/**
 * Create a new MySQL connection.
 */
export const connection = createConnection(mysql);
connection.connect();

/**
 * Create a new Microservice instance.
 */
const service = Microservice.host(8080);
service.setDefiningRoute('/api', route.register());
```

{% endcode %}

**Step 3:** Make a separate file for all your routes to join, then later import that route in your microservice using **#setDefiningRoute()** as shown above.

Then create a new router and export it when you're finished adding all the sub-routes.

{% code title="routes.ts" %}

```typescript
import { Router } from '@spigotmc/microservice';
import { playerService } from './services/player.service';

/**
 * Create a new Router from the Microservice library.
 */ 
const router = new Router();

/**
 * Add any sub-routes to the main router.
 */
router.use('/player', playerService);

/**
 * Export the router so that we can use
 * it in the main class where you decl-
 * ared the microservice.
 */ 
export default router;
```

{% endcode %}
{% endtab %}
{% endtabs %}

With that, the API has been successfully initialized on both your Java plugin and Node.js platform.
