# Create a portal session

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/v2/pcv-1/idempotency)

Creates a portal session for a customer. The session resource in the response contains the access URL. Forward the customer to that access URL. If you would like to logout the customer later via API call, you need to store the id of the portal session resource returned by this API. While creating a session, you also need to pass the redirect URL to which your customers will be sent to upon logout from the portal UI.

## Sample Request

#### Chargebee CLI

```bash
chargebee portal-session create \
    -d 'customer[id]=__test__KyVnHhSBWm8kA2sT' \
    -d redirect_url=https://yourdomain.com/users/3490343
```

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/portal_sessions \
     -u {site_api_key}:\
     -d "customer[id]"="__test__KyVnHhSBWm8kA2sT" \
     -d redirect_url="https://yourdomain.com/users/3490343"
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = PortalSession.Create()
		.CustomerId("__test__KyVnHhSBWm8kA2sT")
		.RedirectUrl("https://yourdomain.com/users/3490343")
		.Request();

PortalSession portalSession = result.PortalSession;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    portalsessionAction "github.com/chargebee/chargebee-go/v3/actions/portalsession"
    "github.com/chargebee/chargebee-go/v3/models/portalsession"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := portalsessionAction.Create(&portalsession.CreateRequestParams{
        Customer : &portalsession.CreateCustomerParams{
            Id : "__test__KyVnHhSBWm8kA2sT",
        },
        RedirectUrl : "https://yourdomain.com/users/3490343",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        PortalSession := res.PortalSession
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.PortalSessionCreateRequest{
    Customer : &chargebee.PortalSessionCreateCustomer{
        Id : "__test__KyVnHhSBWm8kA2sT",
    },
    RedirectUrl : "https://yourdomain.com/users/3490343",
}
  res, err := client.PortalSession.Create(req)
      if err != nil {
        fmt.Println(err)
    } else {
        PortalSession := res.PortalSession
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = PortalSession.create()
            .customerId("__test__KyVnHhSBWm8kA2sT")
            .redirectUrl("https://yourdomain.com/users/3490343")
            .request();

        PortalSession portalSession = result.portalSession();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.portalSession.PortalSession;
import com.chargebee.v4.models.portalSession.params.PortalSessionCreateParams;
import com.chargebee.v4.models.portalSession.responses.PortalSessionCreateResponse;

public class PortalSessionCreate {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        PortalSessionCreateParams.CustomerParams customerParams =
            PortalSessionCreateParams.CustomerParams.builder()
                .id("__test__KyVnHhSBWm8kA2sT")
                .build();

        PortalSessionCreateParams params = PortalSessionCreateParams.builder()
            .customer(customerParams)
            .redirectUrl("https://yourdomain.com/users/3490343")
            .build();

        PortalSessionCreateResponse response = client.portalSessions().create(params);

        PortalSession portalSession = response.getPortalSession();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.portalSession.create({
        customer: {
            id: "__test__KyVnHhSBWm8kA2sT"
        },
        redirect_url: "https://yourdomain.com/users/3490343"
    });

    console.log(result);
    const portalSession = result.portal_session;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->portalSession()->create([
    "customer" => [
        "id" => "__test__KyVnHhSBWm8kA2sT"
    ],
    "redirect_url" => "https://yourdomain.com/users/3490343"
]);
$portalSession = $result->portal_session;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.PortalSession.create(
    cb_client.PortalSession.CreateParams(
        customer=cb_client.PortalSession.CreateCustomerParams(
            id="__test__KyVnHhSBWm8kA2sT"
        ),
        redirect_url="https://yourdomain.com/users/3490343"
    )
)
portal_session = response.portal_session
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::PortalSession.create({
  :customer => {
    :id => "__test__KyVnHhSBWm8kA2sT"
  },
  :redirect_url => "https://yourdomain.com/users/3490343"
})

portal_session = result.portal_session
```

## Sample Response

```json
{
  "portal_session": {
    "access_url": "https://yourapp.chargebeeportal.com/portal/access/__test__cdBbQgJg5fPSTw9qBPDIaQiVEEx0Gsrrc",
    "created_at": 1517505973,
    "customer_id": "__test__KyVnHhSBWm8kA2sT",
    "expires_at": 1517509573,
    "id": "portal___test__KyVnHhSBWm8l82sV",
    "linked_customers": [
      {
        "customer_id": "__test__KyVnHhSBWm8kA2sT",
        "has_active_subscription": false,
        "has_billing_address": false,
        "has_payment_method": false,
        "object": "linked_customer"
      },
      {..}
    ],
    "object": "portal_session",
    "redirect_url": "https://yourdomain.com/users/3490343",
    "status": "created",
    "token": "__test__cdBbQgJg5fPSTw9qBPDIaQiVEEx0Gsrrc"
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/portal_sessions

## Input Parameters

- `redirect_url` (Required if customer portal is not integrated via iframe, string, max chars=250)
  URL to redirect when the user logs out from the portal.

- `forward_url` (optional, string, max chars=250)
  By default access\_url redirects the customer to the portal home page. If you would like to redirect the customer to a different URL, you can use this parameter to do so.
  
  **Note:** This parameter is not applicable for [in-app](https://www.chargebee.com/docs/v3-self-serve-portal.html) portal.

- `customer` (optional, string)
  Parameters for customer
  - `id` (required, string, max chars=50)
    Identifier of the customer.

## Returns

- `portal_session` (Portal session object)
  Resource object representing portal\_session
