# Getting Started

Interact with PayPI using our SDKs:

* [Node JS](https://github.com/Paypi/paypi-node)
* [Golang](https://github.com/Paypi/paypi-go)
* [Python](https://github.com/paypi/paypi-python)

Get started creating an API or check out some of our example implementations:

* [Go Yoda](https://github.com/Paypi/go-yoda) (A text to Yoda speak API in Go)
* [Express Yoda](https://github.com/Paypi/express-yoda) (A text to Yoda speak API in NodeJS)
* [EmailVerify](https://emailverify.paypi.dev/) (Email verification API from PayPI)

## 1. Setup Partner Account

To setup a PayPI team to become a partner, switch to that team and click "Sell on PayPI", this will allow you access to the API Manager via the marketplace icon in the main menu, where you can start creating and managing your APIs.

![](https://4194802648-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MahdtaeM7V2znnnLzXL%2F-Me9bkwFVTaC5QMhk1r7%2F-Me9cZ5GvKtwtZdhjC_S%2FScreenshot%202021-07-09%20at%2010.37.50.png?alt=media\&token=a1f6670f-50a4-4163-ae07-a13e4cb60771)

## 2. Create an API

Create an API on the PayPI website and keep note of your [API Secret](https://partner.paypi.dev/api/api#api-secret) (you'll use this to authenticate your API) and your [Share URL](https://partner.paypi.dev/api/api#share-url) (your users need this to subscribe to your API).

### Create a charge

To accept payments you need to decide what to charge your users. Charges are created in the “charges” section of your API dashboard, they state what you are going to be charging your users for and how much.

We recommend the following when creating charges:

* [x] Make it simple, keep the number of charges to a minimum.
* [x] Write a detailed description so users know exactly what they’re being charged for.
* [x] Use dynamic charges wherever your charge may change per request, like charging more per request

## 3. Setup your API Application

Install PayPI's library, where you plan to do your authentication.&#x20;

{% tabs %}
{% tab title="Go" %}

```bash
go get -u github.com/paypi/paypi-go
```

{% endtab %}

{% tab title="Node JS" %}

```javascript
npm install paypi
yarn add paypi
```

{% endtab %}

{% tab title="Python" %}

```
pip install paypi
```

{% endtab %}
{% endtabs %}

### Setup API

Import the PayPI library and set your [API Secret](https://partner.paypi.dev/api/api#api-secret).&#x20;

{% tabs %}
{% tab title="Go" %}

```go
import (
  "http"
  "github.com/paypi/paypi-go/paypi"
)

paypi.Key = "<API_SECRET_KEY>"
```

{% endtab %}

{% tab title="Node JS" %}

```javascript
import PayPI from 'paypi'

const paypi = new PayPI("<Your API secret")
```

{% endtab %}

{% tab title="Python" %}

```python
from paypi import PayPI

paypi = PayPI("<Your API Secret>")
```

{% endtab %}
{% endtabs %}

### Authenticate users

To take payments, you must first check that the API token they have given you is valid. &#x20;

{% hint style="info" %}
You may retrieve the API token from your users in any way you see fit, although we recommend this is done in the Authentication header if HTTP is being used.
{% endhint %}

{% tabs %}
{% tab title="Go" %}

```go
user, err := paypi.Authenticate("<Subscriber secret>")
if err != nil {
  http.Error(w, "User token is unauthorized", http.StatusUnauthorized)
  return
}
```

{% endtab %}

{% tab title="Node JS" %}

```javascript
const user = await paypi.authenticate("<Subscriber secret>")
```

{% endtab %}

{% tab title="Python" %}

```python
# Async 
user = await paypi.authenticate("<Users Subscription Secret>")

# Synchronously
user = paypi.authenticate_sync("Users Subscription Secret")
```

{% endtab %}
{% endtabs %}

If this method returns without error you can continue processing the request. If it fails you should immediately return an unauthorised response from your API.&#x20;

## 4. Make Charges

{% tabs %}
{% tab title="Go" %}
`user.MakeCharge` is used to charge users on the platform. When `user.MakeCharge` is called the user is charged for their usage on your platform. This should be done at the end of your request, when you are sure that their request is going to be fulfilled successfully.

#### **Static Charges**

Static charges need only provide the charge identifier

```go
user, _ := paypi.Authenticate("<USER_TOKEN>")

charge, err := user.MakeCharge(paypi.MakeChargeInput{
  ChargeIdentifier: "<CHARGE_IDENTIFIER>",
})
```

**Dynamic Charges**

If a charge is dynamic, you must provide the number of units used. This is usually representative of seconds of processing time, or MB of data processed. This unit is multiplied by the price set for the dynamic charge to calculate the total cost to the user.

```go
user, _ := paypi.Authenticate("<USER_TOKEN>")

charge, err := user.MakeCharge(paypi.MakeChargeInput{
  ChargeIdentifier: "<CHARGE_IDENTIFIER>",
  UnitsUsed: 5,
})
```

{% endtab %}

{% tab title="Node JS" %}
`user.makeCharge` is used to charge users on the platform. When `user.makeCharge` is called the user is charged for their usage on your platform. This should be done at the end of your request, when you are sure that their request is going to be fulfilled successfully.

#### **Static Charges**

Static charges need only provide the charge identifier

```javascript
const user = await paypi.authenticate("<Subscriber secret>")

await user.makeCharge("<CHARGE_IDENTIFIER>")
```

**Dynamic Charges**

If a charge is dynamic, you must provide the number of units used. This is usually representative of seconds of processing time, or MB of data processed. This unit is multiplied by the price set for the dynamic charge to calculate the total cost to the user.

```javascript
const user = await paypi.authenticate("<Subscriber secret>")

const unitsUsed = 4
await user.makeCharge("<CHARGE_IDENTIFIER>", unitsUsed)
```

{% endtab %}

{% tab title="Python" %}
`user.make_charge` is used to charge users on the platform. When `user.make_charge` is called the user is charged for their usage on your platform. This should be done at the end of your request, when you are sure that their request is going to be fulfilled successfully.

#### **Static Charges**

Static charges need only provide the charge identifier

```python
# Async
user = await paypi.authenticate("<Subscription Secret>")
await user.make_charge("<CHARGE_IDENTIFIER>")

# Sync
user = paypi.authenticate_sync("<Subscription Secret>")
user.make_charge_sync("<CHARGE_IDENTIFIER>")
```

**Dynamic Charges**

If a charge is dynamic, you must provide the number of units used. This is usually representative of seconds of processing time, or MB of data processed. This unit is multiplied by the price set for the dynamic charge to calculate the total cost to the user.

```python
units_used = 4

# Async
user = await paypi.authenticate("<Subscription Secret>")
await user.make_charge("<CHARGE_IDENTIFIER>", units_used)

# Sync
user = paypi.authenticate_sync("<Subscription Secret>")
user.make_charge_sync("<CHARGE_IDENTIFIER>", units_used)
```

{% endtab %}
{% endtabs %}

### Example Usage

{% tabs %}
{% tab title="Go" %}

```go
package main

import (
  "http"
  "github.com/paypi/paypi-go/paypi"
)

// Set the API secret key on initialising your application
paypi.Key = "<API_SECRET_KEY>"

func handleRequest(w http.ResponseWriter, r *http.Request) {
  // Check if a user's token is valid, this token usually comes from the
  // Authorization header, but can be given in any way you choose.
  user, err := paypi.Authenticate(r.Header.Get("Authorization"))
  if err != nil {
    http.Error(w, "User token is unauthorized", http.StatusUnauthorized)
    return
  }

  // Do some processing, fetch the response etc...

  // If request was successful, make a static charge
  charge, err := user.MakeCharge(paypi.MakeChargeInput{
    ChargeIdentifier: "<STATIC_CHARGE_IDENTIFIER>",
  })

  // Make a dynamic charge based on processing 
  charge, err = user.MakeCharge(paypi.MakeChargeInput{
    ChargeIdentifier: "<DYNAMIC_CHARGE_IDENTIFIER>",
    UnitsUsed: 2.34,
  })

  fmt.Fprintf(w, "Request Successful")
}
```

{% endtab %}

{% tab title="Node JS" %}

```javascript
import PayPI from 'paypi';
import express from 'express'

const app = express()
const port = 3000
const paypi = new PayPI("<YOUR API SECRET>")

app.get('/', async (req, res) => {
  const subscriberSecret = req.get("Authentication")
  const user = await paypi.authenticate(subscriberSecret)
  
  // Do some processing, fetch response data, etc
  
  // Once request is going to go through, charge the user using a ChargeID.
  await user.makeCharge("cid-R4tfSt4")
  await user.makeCharge("cid-U7dhaf3", 34) // Dynamic charges need to be given unitsUsed.
  
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
```

{% endtab %}

{% tab title="Python" %}

```python
from paypi import PayPI

paypi = PayPI("<Your API Secret>")


@app.route("/")
async def hello():
    user = await paypi.authenticate("<Users Subscription Secret>")

    # do some work...

    await user.make_charge("<Charge ID>")
    
    # charge is now made, return the response
```

{% endtab %}
{% endtabs %}

## 5. Share your API&#x20;

Inform your users they can pay with PayPI by redirecting them to the [Share URL](https://partner.paypi.dev/api/api#share-url) link for your API.
